Explorar el Código

Switch earlier solutions to use python3, tidy.

master
Chris Smith hace 7 años
padre
commit
92c6b3ff46
Se han modificado 7 ficheros con 25 adiciones y 25 borrados
  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 Ver fichero

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

+ 2
- 2
02.py Ver fichero

1
-#!/usr/bin/python
1
+#!/usr/bin/python3
2
 
2
 
3
 import operator
3
 import operator
4
 
4
 
17
                                        (2, 4): 'D'}}
17
                                        (2, 4): 'D'}}
18
 
18
 
19
 for part, keys in parts.items():
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
     ans = ''
21
     ans = ''
22
     for line in input:
22
     for line in input:
23
         for move in map(dirs.get, line):
23
         for move in map(dirs.get, line):

+ 1
- 1
03.py Ver fichero

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

+ 1
- 1
04.py Ver fichero

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

+ 15
- 13
09.py Ver fichero

5
 # Version of parse() that recursively parses repeated sections
5
 # Version of parse() that recursively parses repeated sections
6
 rparse = lambda x: parse(x, rparse)
6
 rparse = lambda x: parse(x, rparse)
7
 
7
 
8
+
8
 def parse(data, rfun=len):
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
 with open('09.txt', 'r') as file:
25
 with open('09.txt', 'r') as file:
24
     input = re.sub('\s+', '', file.read())
26
     input = re.sub('\s+', '', file.read())

+ 2
- 4
11.py Ver fichero

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

+ 1
- 1
13.py Ver fichero

8
 wall = lambda x, y: high(x*x + 3*x + 2*x*y + y + y*y + input) % 2 == 1
8
 wall = lambda x, y: high(x*x + 3*x + 2*x*y + y + y*y + input) % 2 == 1
9
 all_moves = lambda x, y: [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
9
 all_moves = lambda x, y: [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
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))
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
 visited = set()
12
 visited = set()
13
 distance = {}
13
 distance = {}
14
 steps = 0
14
 steps = 0

Loading…
Cancelar
Guardar