Browse Source

Style fixes

master
Chris Smith 7 years ago
parent
commit
2195b89d7b
5 changed files with 10 additions and 7 deletions
  1. 1
    1
      04.py
  2. 2
    0
      05.py
  3. 3
    3
      08.py
  4. 2
    1
      09.py
  5. 2
    2
      11.py

+ 1
- 1
04.py View File

9
     checksum = lambda x: re.sub(r'(.)\1+', r'\1',
9
     checksum = lambda x: re.sub(r'(.)\1+', r'\1',
10
                                 ''.join(sorted(x.name.replace('-', ''),
10
                                 ''.join(sorted(x.name.replace('-', ''),
11
                                                key=lambda c: -128 * x.name.count(c) + ord(c)))
11
                                                key=lambda c: -128 * x.name.count(c) + ord(c)))
12
-                               )[:5]
12
+                                )[:5]
13
     valid = [room for room in rooms if room.checksum == checksum(room)]
13
     valid = [room for room in rooms if room.checksum == checksum(room)]
14
     print("Part one: %s" % sum([int(room.sector) for room in valid]))
14
     print("Part one: %s" % sum([int(room.sector) for room in valid]))
15
 
15
 

+ 2
- 0
05.py View File

6
 
6
 
7
 door = 'abbhdwsy'
7
 door = 'abbhdwsy'
8
 
8
 
9
+
9
 def hash(x):
10
 def hash(x):
10
     return hashlib.md5((door + str(x)).encode('utf-8')).hexdigest()
11
     return hashlib.md5((door + str(x)).encode('utf-8')).hexdigest()
11
 
12
 
13
+
12
 pool = Pool()
14
 pool = Pool()
13
 hashes = pool.imap(hash, itertools.count(), 10000)
15
 hashes = pool.imap(hash, itertools.count(), 10000)
14
 zeroes = (h for h in hashes if h.startswith('00000'))
16
 zeroes = (h for h in hashes if h.startswith('00000'))

+ 3
- 3
08.py View File

14
         elif words[1] == 'row':
14
         elif words[1] == 'row':
15
             lights[i] = np.roll(lights[i], j)
15
             lights[i] = np.roll(lights[i], j)
16
         else:
16
         else:
17
-            lights[:,i] = np.roll(lights[:,i], j)
18
-        
17
+            lights[:, i] = np.roll(lights[:, i], j)
18
+
19
     print("Part one: %s" % np.sum(lights))
19
     print("Part one: %s" % np.sum(lights))
20
     print("Part two:")
20
     print("Part two:")
21
-    print('\n'.join(''.join('\u2588' if p else ' '  for p in row) for row in lights))
21
+    print('\n'.join(''.join('\u2588' if p else ' ' for p in row) for row in lights))

+ 2
- 1
09.py View File

9
 def parse(data, rfun=len):
9
 def parse(data, rfun=len):
10
     # Find the first bracketed part (if one exists)
10
     # Find the first bracketed part (if one exists)
11
     index, bracket = data.find('('), data.find(')')
11
     index, bracket = data.find('('), data.find(')')
12
-    if index == -1: return len(data)
12
+    if index == -1:
13
+        return len(data)
13
 
14
 
14
     # Grab the (NxR) values from the brackets
15
     # Grab the (NxR) values from the brackets
15
     num, reps = map(int, data[index + 1:bracket].split('x'))
16
     num, reps = map(int, data[index + 1:bracket].split('x'))

+ 2
- 2
11.py View File

34
 
34
 
35
 # Returns an enumeration of possible destinations for the lift (up or down one floor)
35
 # Returns an enumeration of possible destinations for the lift (up or down one floor)
36
 dests = lambda layout: filter(is_floor, [my_floor_index(layout) + 1, my_floor_index(layout) - 1])
36
 dests = lambda layout: filter(is_floor, [my_floor_index(layout) + 1, my_floor_index(layout) - 1])
37
-is_floor = lambda i: i >= 0 and i < len(floors)
37
+is_floor = lambda i: 0 <= i < len(floors)
38
 
38
 
39
 # Returns an enumeration of possible moves that could be made from the given state
39
 # Returns an enumeration of possible moves that could be made from the given state
40
 moves = lambda layout: itertools.product(pickups(items(my_floor(layout))), dests(layout))
40
 moves = lambda layout: itertools.product(pickups(items(my_floor(layout))), dests(layout))
74
         for items, to in moves(layout):
74
         for items, to in moves(layout):
75
             items = set(items).union({lift})
75
             items = set(items).union({lift})
76
             new_layout = [set(floor) - items for floor in layout]
76
             new_layout = [set(floor) - items for floor in layout]
77
-            new_layout[to] |= (items)
77
+            new_layout[to] |= items
78
             if valid_layout(new_layout):
78
             if valid_layout(new_layout):
79
                 serialised = serialise(new_layout)
79
                 serialised = serialise(new_layout)
80
                 if serialised not in distances:
80
                 if serialised not in distances:

Loading…
Cancel
Save