Browse Source

Switch earlier solutions to use python3, tidy.

master
Chris Smith 7 years ago
parent
commit
92c6b3ff46
7 changed files with 25 additions and 25 deletions
  1. 3
    3
      01.py
  2. 2
    2
      02.py
  3. 1
    1
      03.py
  4. 1
    1
      04.py
  5. 15
    13
      09.py
  6. 2
    4
      11.py
  7. 1
    1
      13.py

+ 3
- 3
01.py View File

@@ -1,9 +1,9 @@
1
-#!/usr/bin/python
1
+#!/usr/bin/python3
2 2
 
3
-from enum import IntEnum
4 3
 import math
5 4
 import operator
6 5
 
6
+
7 7
 def steps(start, delta, count):
8 8
     sign = int(math.copysign(1, delta))
9 9
     return [start] * count if delta == 0 else range(start + sign, start + delta + sign, sign)
@@ -20,7 +20,7 @@ heading = 0
20 20
 history = [(0, 0)]
21 21
 for move in moves:
22 22
     heading = (heading + move[0]) % 4
23
-    delta = map(operator.mul, direction_lookup[heading], (move[1], move[1]))
23
+    delta = list(map(operator.mul, direction_lookup[heading], (move[1], move[1])))
24 24
     delta_mag = max(abs(delta[0]), abs(delta[1]))
25 25
     history += zip(steps(history[-1][0], delta[0], delta_mag),
26 26
                    steps(history[-1][1], delta[1], delta_mag))

+ 2
- 2
02.py View File

@@ -1,4 +1,4 @@
1
-#!/usr/bin/python
1
+#!/usr/bin/python3
2 2
 
3 3
 import operator
4 4
 
@@ -17,7 +17,7 @@ parts = {1: {(0, 0): '1', (1, 0): '2', (2, 0): '3',
17 17
                                        (2, 4): 'D'}}
18 18
 
19 19
 for part, keys in parts.items():
20
-    pos = keys.keys()[keys.values().index('5')]
20
+    pos = list(keys.keys())[list(keys.values()).index('5')]
21 21
     ans = ''
22 22
     for line in input:
23 23
         for move in map(dirs.get, line):

+ 1
- 1
03.py View File

@@ -1,4 +1,4 @@
1
-#!/usr/bin/python
1
+#!/usr/bin/python3
2 2
 
3 3
 from itertools import chain
4 4
 

+ 1
- 1
04.py View File

@@ -1,4 +1,4 @@
1
-#!/usr/bin/python
1
+#!/usr/bin/python3
2 2
 
3 3
 import re
4 4
 from collections import namedtuple

+ 15
- 13
09.py View File

@@ -5,20 +5,22 @@ import re
5 5
 # Version of parse() that recursively parses repeated sections
6 6
 rparse = lambda x: parse(x, rparse)
7 7
 
8
+
8 9
 def parse(data, rfun=len):
9
-	# Find the first bracketed part (if one exists)
10
-	index, bracket = data.find('('), data.find(')')
11
-	if index == -1: return len(data)
12
-
13
-	# Grab the (NxR) values from the brackets
14
-	num, reps = map(int, data[index+1:bracket].split('x'))
15
-	
16
-	# Return the initial, non-repeated data, plus...
17
-	return (len(data[:index])
18
-			# the repeated data, potentially parsed recursively, plus...
19
-	        + reps * rfun(data[bracket+1:bracket+1+num]) 
20
-	        # the remainder of the string, parsed
21
-	        + parse(data[bracket+1+num:], rfun))
10
+    # Find the first bracketed part (if one exists)
11
+    index, bracket = data.find('('), data.find(')')
12
+    if index == -1: return len(data)
13
+
14
+    # Grab the (NxR) values from the brackets
15
+    num, reps = map(int, data[index + 1:bracket].split('x'))
16
+
17
+    # Return the initial, non-repeated data, plus...
18
+    return (len(data[:index])
19
+            # the repeated data, potentially parsed recursively, plus...
20
+            + reps * rfun(data[bracket + 1:bracket + 1 + num])
21
+            # the remainder of the string, parsed
22
+            + parse(data[bracket + 1 + num:], rfun))
23
+
22 24
 
23 25
 with open('09.txt', 'r') as file:
24 26
     input = re.sub('\s+', '', file.read())

+ 2
- 4
11.py View File

@@ -27,7 +27,7 @@ my_floor = lambda layout: next(floor for floor in layout if lift in floor)
27 27
 my_floor_index = lambda layout: next(i for i, floor in enumerate(layout) if lift in floor)
28 28
 
29 29
 # Returns just the items on a floor (not the lift)
30
-items = lambda floor: set(floor) - set([lift])
30
+items = lambda floor: set(floor) - {lift}
31 31
 
32 32
 # Returns an enumeration of sets of items that could potentially be picked up (any combo of 1 or 2 items)
33 33
 pickups = lambda items: map(set, itertools.chain(itertools.combinations(items, 2), itertools.combinations(items, 1)))
@@ -66,14 +66,13 @@ def run(floors):
66 66
             mappings['%s-compatible microchip' % key] = '%iM' % i
67 67
         return '|'.join(''.join(sorted(mappings[item] for item in floor)) for floor in layout)
68 68
 
69
-
70 69
     # Evaluates each possible move for the given layout.
71 70
     # Moves are checked to ensure they're valid and serialised to ensure they haven't been visited
72 71
     # Returns a list of new layouts for the next step, or False if a solution was encountered
73 72
     def domoves(layout, steps):
74 73
         queued = []
75 74
         for items, to in moves(layout):
76
-            items = set(items).union(set([lift]))
75
+            items = set(items).union({lift})
77 76
             new_layout = [set(floor) - items for floor in layout]
78 77
             new_layout[to] |= (items)
79 78
             if valid_layout(new_layout):
@@ -86,7 +85,6 @@ def run(floors):
86 85
                         return False
87 86
         return queued
88 87
 
89
-
90 88
     # Run repeated iterations until we hit a winning result, then immediately returns the step
91 89
     # count.
92 90
     distances = {serialise(floors): 0}

+ 1
- 1
13.py View File

@@ -8,7 +8,7 @@ high = lambda x: sum([1 for d in bin(x) if d == '1'])
8 8
 wall = lambda x, y: high(x*x + 3*x + 2*x*y + y + y*y + input) % 2 == 1
9 9
 all_moves = lambda x, y: [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
10 10
 moves = lambda x, y: (m for m in all_moves(x, y) if m[0] >= 0 and m[1] >= 0 and not wall(*m))
11
-queue = set([(1, 1)])
11
+queue = {(1, 1)}
12 12
 visited = set()
13 13
 distance = {}
14 14
 steps = 0

Loading…
Cancel
Save