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/25

+109
+46
2017/day25.py
··· 1 + import fileinput 2 + from collections import defaultdict 3 + 4 + from utils import parse_line 5 + 6 + PROG = {} 7 + 8 + # Parse puzzle input 9 + for i, line in enumerate(fileinput.input()): 10 + if i == 0: 11 + STATE = parse_line(r'Begin in state (\w+).', line)[0] 12 + elif i == 1: 13 + STEPS = int(parse_line(r'Perform a diagnostic checksum after (\d+) steps.', line)[0]) 14 + 15 + if i <= 2: 16 + continue 17 + 18 + if i % 10 == 3: 19 + start = parse_line(r'In state (\w+):', line)[0] 20 + elif i % 10 == 5: 21 + write_0 = parse_line(r' - Write the value (\d+).', line)[0] 22 + elif i % 10 == 6: 23 + move_0 = parse_line(r' - Move one slot to the (\w+).', line)[0] 24 + move_0 = 1 if move_0 == 'right' else -1 25 + elif i % 10 == 7: 26 + next_0 = parse_line(r' - Continue with state (\w+).', line)[0] 27 + elif i % 10 == 9: 28 + write_1 = parse_line(r' - Write the value (\d+).', line)[0] 29 + elif i % 10 == 0: 30 + move_1 = parse_line(r' - Move one slot to the (\w+).', line)[0] 31 + move_1 = 1 if move_1 == 'right' else -1 32 + elif i % 10 == 1: 33 + next_1 = parse_line(r' - Continue with state (\w+).', line)[0] 34 + PROG[start] = ((write_0, move_0, next_0), (write_1, move_1, next_1)) 35 + 36 + 37 + TAPE = defaultdict(int) 38 + CURSOR = 0 39 + 40 + for _ in range(STEPS): 41 + write, dx, ns = PROG[STATE][TAPE[CURSOR]] 42 + TAPE[CURSOR] = write 43 + CURSOR += dx 44 + STATE = ns 45 + 46 + print "Diagnostic checksum:", sum(x for x in TAPE.values())
+62
2017/inputs/25.txt
··· 1 + Begin in state A. 2 + Perform a diagnostic checksum after 12667664 steps. 3 + 4 + In state A: 5 + If the current value is 0: 6 + - Write the value 1. 7 + - Move one slot to the right. 8 + - Continue with state B. 9 + If the current value is 1: 10 + - Write the value 0. 11 + - Move one slot to the left. 12 + - Continue with state C. 13 + 14 + In state B: 15 + If the current value is 0: 16 + - Write the value 1. 17 + - Move one slot to the left. 18 + - Continue with state A. 19 + If the current value is 1: 20 + - Write the value 1. 21 + - Move one slot to the right. 22 + - Continue with state D. 23 + 24 + In state C: 25 + If the current value is 0: 26 + - Write the value 0. 27 + - Move one slot to the left. 28 + - Continue with state B. 29 + If the current value is 1: 30 + - Write the value 0. 31 + - Move one slot to the left. 32 + - Continue with state E. 33 + 34 + In state D: 35 + If the current value is 0: 36 + - Write the value 1. 37 + - Move one slot to the right. 38 + - Continue with state A. 39 + If the current value is 1: 40 + - Write the value 0. 41 + - Move one slot to the right. 42 + - Continue with state B. 43 + 44 + In state E: 45 + If the current value is 0: 46 + - Write the value 1. 47 + - Move one slot to the left. 48 + - Continue with state F. 49 + If the current value is 1: 50 + - Write the value 1. 51 + - Move one slot to the left. 52 + - Continue with state C. 53 + 54 + In state F: 55 + If the current value is 0: 56 + - Write the value 1. 57 + - Move one slot to the right. 58 + - Continue with state D. 59 + If the current value is 1: 60 + - Write the value 1. 61 + - Move one slot to the right. 62 + - Continue with state A.
+1
2017/outputs/25.txt
··· 1 + 4769