Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to Day 8: I Heard You Like Registers - Part 2
#aoc_d8.py
import re
inp = ["b inc 5 if a > 1",
"a inc 1 if b < 5",
"c dec -10 if a >= 1",
"c inc -20 if c == 10"]
inp = open("input_d8.txt","r").readlines()
ops = {">":lambda x,v: x>v,"<":lambda x,v: x<v, "inc":lambda x,v: x+v,"<=": lambda x,v: x<=v, "!=": lambda x,v: x!=v, ">=": lambda x,v: x>=v, "==": lambda x,v: x==v, "dec":lambda x,v: x-v}
regs = dict()
max_v = 0
for l in inp:
m = re.match("([a-z]+)\s(inc|dec)\s(-?\d+)\sif\s([a-z]+)\s(<|>|!=|==|>=|<=)\s(-?\d+)",l)
if not m:
print l
#print m
reg_to_mod = m.group(1)
op = m.group(2)
val_to_mod = int(m.group(3))
reg_to_check = m.group(4)
op_to_check = m.group(5)
value_to_check = int(m.group(6))
#print reg_to_mod, op, val_to_mod, reg_to_check, op_to_check, value_to_check
if reg_to_mod not in regs:
regs[reg_to_mod] = 0
if reg_to_check not in regs:
regs[reg_to_check] = 0
#print "Result:", ops[op_to_check](regs[reg_to_check],value_to_check)
if ops[op_to_check](regs[reg_to_check],value_to_check):
regs[reg_to_mod] = ops[op](regs[reg_to_mod],val_to_mod)
if regs[reg_to_mod] > max_v:
max_v = regs[reg_to_mod]
#print regs
#print regs
r = max(regs, key=lambda i: regs[i])
print r,"=",regs[r], "max all time=", max_v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment