Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to Day 23: Coprocessor Conflagration
#!/bin/python
#aoc_d23.py
inp = [
"set b 93",
"set c b",
"jnz a 2",
"jnz 1 5",
"mul b 100",
"sub b -100000",
"set c b",
"sub c -17000",
"set f 1",
"set d 2",
"set e 2",
"set g d",
"mul g e",
"sub g b",
"jnz g 2",
"set f 0",
"sub e -1",
"set g e",
"sub g b",
"jnz g -8",
"sub d -1",
"set g d",
"sub g b",
"jnz g -13",
"jnz f 2",
"sub h -1",
"set g b",
"sub g c",
"jnz g 2",
"jnz 1 3",
"sub b -17",
"jnz 1 -23"]
#inp = open('input_d18.txt','r').readlines()
regs = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0}
last_freq = 0
pc = 0
import re
muls = 0
while True:
if pc >= len(inp):
break
i = inp[pc]
print pc,"=>",i, regs
if i.startswith("set"):
m = re.match("set\s([a-z])\s(-?\d+)",i)
if m:
regs[m.group(1)] = int(m.group(2))
else:
m = re.match("set\s([a-z])\s([a-z])",i)
regs[m.group(1)] = regs[m.group(2)]
if i.startswith("sub"):
m = re.match("sub\s([a-z])\s(-?\d+)",i)
if m:
regs[m.group(1)] -= int(m.group(2))
else:
m = re.match("sub\s([a-z])\s([a-z])",i)
regs[m.group(1)] -= regs[m.group(2)]
if i.startswith("mul"):
muls +=1
m = re.match("mul\s([a-z])\s([a-z])",i)
if m:
regs[m.group(1)] *= regs[m.group(2)]
else:
m = re.match("mul\s([a-z])\s(-?\d+)",i)
regs[m.group(1)] *= int(m.group(2))
if i.startswith("jnz"):
m = re.match("jnz\s([a-z])\s(-?\d+)",i)
if m:
if regs[m.group(1)] != 0:
pc += int(m.group(2))
continue
else:
m = re.match("jnz\s(-?\d+)\s(-?\d+)",i)
if m.group(1) != 0:
pc += int(m.group(2))
continue
pc += 1
if pc >= len(inp):
break
print regs
print 'Muls: ',muls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment