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

+91
+57
2017/day23.py
··· 1 + import math 2 + import fileinput 3 + from collections import defaultdict 4 + 5 + from utils import parse_line, mul, all_unique, factors, memoize, primes # NOQA 6 + 7 + INSTRS = [] 8 + B = None 9 + 10 + # Read puzzle input 11 + for line in fileinput.input(): 12 + instr = line.strip() + ' foo' 13 + op, x, y = instr.split()[:3] 14 + INSTRS.append((op, x, y)) 15 + 16 + if fileinput.isfirstline(): 17 + B = int(y) 18 + 19 + # Part 1 20 + REGS = defaultdict(int) 21 + pc = 0 22 + muls = 0 23 + 24 + while 0 <= pc < len(INSTRS): 25 + op, x, y = INSTRS[pc] 26 + y = REGS[y] if y.isalpha() else int(y) 27 + 28 + if op == 'set': 29 + REGS[x] = y 30 + elif op == 'sub': 31 + REGS[x] -= y 32 + elif op == 'mul': 33 + REGS[x] *= y 34 + muls += 1 35 + elif op == 'mod': 36 + REGS[x] %= y 37 + elif op == 'jnz': 38 + if (REGS[x] if x.isalpha() else int(x)) != 0: 39 + pc += y 40 + continue 41 + 42 + pc += 1 43 + 44 + print "`mul` instruction invocations:", muls 45 + 46 + # Part 2 47 + b = (B * 100) + 100000 48 + c = b + 17000 49 + h = 0 50 + 51 + for n in range(b, c + 1, 17): 52 + for i in range(2, int(math.sqrt(n))): 53 + if n % i == 0: 54 + h += 1 55 + break 56 + 57 + print "Value in register h, ie. primes in [{}, {}]: {}".format(b, c, h)
+32
2017/inputs/23.txt
··· 1 + set b 57 2 + set c b 3 + jnz a 2 4 + jnz 1 5 5 + mul b 100 6 + sub b -100000 7 + set c b 8 + sub c -17000 9 + set f 1 10 + set d 2 11 + set e 2 12 + set g d 13 + mul g e 14 + sub g b 15 + jnz g 2 16 + set f 0 17 + sub e -1 18 + set g e 19 + sub g b 20 + jnz g -8 21 + sub d -1 22 + set g d 23 + sub g b 24 + jnz g -13 25 + jnz f 2 26 + sub h -1 27 + set g b 28 + sub g c 29 + jnz g 2 30 + jnz 1 3 31 + sub b -17 32 + jnz 1 -23
+2
2017/outputs/23.txt
··· 1 + 3025 2 + 915