2017-12-23 - Coprocessor Conflagration
(original .ipynb)puzzle_input_str = open("puzzle_input/day23.txt").read() def val(regs, v): if (v.isalpha()): return regs[v] return int(v) def set_reg(regs, x, y): regs[x] = val(regs, y) return 1 def sub(regs, x, y): regs[x] -= val(regs, y) return 1 def mul(regs, x, y): regs[x] *= val(regs, y) return 1 def jnz(regs, x, y): if val(regs,x) != 0: return val(regs, y) return 1 def parse_program(program_str): return [ line.split(" ") for line in program_str.split("\n") ] instructions = { "set": set_reg, "sub": sub, "mul": mul, "jnz": jnz } def part_one(input_str): pc = 0 program = parse_program(input_str) regs = { r:0 for r in "abcdefgh" } mul_count = 0 while pc >= 0 and pc < len(program): instr, x, y = program[pc] if instr == "mul": mul_count += 1 pc += instructions[instr](regs, x, y) return mul_count print("part one:", part_one(puzzle_input_str))
part one: 3969
def memoize(func): cache = [] def memoizer(n): nonlocal cache if n > len(cache): cache = func(n) return cache return memoizer @memoize def get_prime(n): prime = [True for i in range(n + 1)] fac = 2 while fac * fac <= n + 1: if prime[fac]: for i in range(fac*fac, n + 1, fac): prime[i] = False fac += 1 return prime def is_prime(n): return get_prime(n)[n]
%%time # my very very raw reverse engineered version of the program # it's a bit slow a, b, c, d, e, f, g, h = 1, 0, 0, 0, 0, 0, 0, 0 b = 65 c = 65 if a != 0: b = 100_000 + (b * 100) c = b + 17_000 g = 1 while g != 0: f = 1 if not is_prime(b): h += 1 g = b - c b += 17 print("part two:", h)
part two: 917 CPU times: user 15.7 s, sys: 32.3 ms, total: 15.8 s Wall time: 16 s