My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2016/13

+47
+46
2016/day13.py
··· 1 + import fileinput 2 + 3 + INPUT = int(fileinput.input()[0]) 4 + 5 + 6 + def is_open(x, y, input=INPUT): 7 + """Open space if number of 1 bits is even, else wall.""" 8 + num = (x * x) + (3 * x) + (2 * x * y) + y + (y * y) + input 9 + return bin(num)[2:].count('1') % 2 == 0 10 + 11 + 12 + def pathfind(a, b, c, d, max_steps=None): 13 + """ 14 + Returns length of the shortest path from (a, b) to (c, d) 15 + and the number of visited locations along the way. 16 + """ 17 + 18 + deltas = [(0, 1), (1, 0), (0, -1), (-1, 0)] 19 + horizon = [(a, b)] 20 + seen = set() 21 + steps = 0 22 + 23 + while horizon: 24 + new_horizon = [] 25 + for x, y in horizon: 26 + if x == c and y == d: 27 + return steps, len(seen) 28 + 29 + seen.add((x, y)) 30 + 31 + for dx, dy in deltas: 32 + nx, ny = x + dx, y + dy 33 + if is_open(nx, ny) and nx >= 0 and ny >= 0 and (nx, ny) not in seen: 34 + new_horizon.append((nx, ny)) 35 + 36 + if steps == max_steps: 37 + break 38 + 39 + horizon = new_horizon 40 + steps += 1 41 + 42 + return steps, len(seen) 43 + 44 + 45 + print "Fewest steps to reach 31, 39 from 1, 1:", pathfind(1, 1, 31, 39)[0] 46 + print "Distinct locations reached in 50 steps:", pathfind(1, 1, -1, -1, max_steps=50)[1]
+1
2016/input13.txt
··· 1 + 1350