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 2017/22

+95
+68
2017/day22.py
··· 1 + import fileinput 2 + from utils import Point, DIRS 3 + 4 + 5 + def simulate_activity(pos, facing, grid, mapping): 6 + node = grid.get(pos, '.') 7 + new_node, df = mapping[node] 8 + 9 + # Update node 10 + grid[pos] = new_node 11 + 12 + # Update direction and position 13 + nf = (facing + df) % 4 14 + pos += DIRS[nf] 15 + 16 + return pos, nf, new_node == '#' 17 + 18 + 19 + # Maps node transformations and turn-direction 20 + PART_1_MAP = { 21 + '#': ('.', -1), 22 + '.': ('#', 1), 23 + } 24 + 25 + PART_2_MAP = { 26 + '.': ('W', 1), 27 + 'W': ('#', 0), 28 + '#': ('F', -1), 29 + 'F': ('.', 2), 30 + } 31 + 32 + GRID = {} 33 + HEIGHT = 0 34 + WIDTH = 0 35 + 36 + # Read puzzle input 37 + for y, line in enumerate(fileinput.input()): 38 + for x, c in enumerate(line.strip()): 39 + GRID[Point(x, y)] = c 40 + 41 + WIDTH = x + 1 42 + HEIGHT += 1 43 + 44 + # Part 1 45 + pos = Point(WIDTH // 2, HEIGHT // 2) 46 + facing = 2 47 + grid = GRID.copy() 48 + infections_caused = 0 49 + 50 + for _ in range(10000): 51 + pos, facing, caused = simulate_activity(pos, facing, grid, PART_1_MAP) 52 + if caused: 53 + infections_caused += 1 54 + 55 + print "Infections caused in Part 1:", infections_caused 56 + 57 + # Part 2 58 + pos = Point(WIDTH // 2, HEIGHT // 2) 59 + facing = 2 60 + grid = GRID.copy() 61 + infections_caused = 0 62 + 63 + for _ in range(10000000): 64 + pos, facing, caused = simulate_activity(pos, facing, grid, PART_2_MAP) 65 + if caused: 66 + infections_caused += 1 67 + 68 + print "Infections caused in Part 2:", infections_caused
+25
2017/inputs/22.txt
··· 1 + .##..#.#.##...#....#..### 2 + ####.#...###.####..#..... 3 + #.#.#####....######.###.# 4 + #.#..###.#.#####....#..#. 5 + ####.#.#...#.##.##..#.### 6 + #.####..#####.#.#....#.## 7 + .#.####.#....###..##....# 8 + ..##.#..##.#.#.###.##.#.. 9 + ##....#....######.###.### 10 + .#.##.###.###.###.#..#.#. 11 + #.##.#.#..#.#.....###.... 12 + ####.....#..###..##..##.. 13 + ##....#.#...####...#.#.#. 14 + ...#.##..###..##..#...... 15 + #....#..##.##.#..#.###..# 16 + ...#...##.##.##...#.#.#.. 17 + .##....#.####.#..##.#...# 18 + #.######......#.#...#.##. 19 + #.##....###...###.###.... 20 + #..#.#.#.#.#..#.#.....#.. 21 + ...##..##.###....#.###... 22 + .######.#...###.###.#.#.# 23 + ####..###.####...#..##### 24 + .##.#.##...##..##...#.#.# 25 + ###...##..#..##.##..#..#.
+2
2017/outputs/22.txt
··· 1 + 5330 2 + 2512103