Skip to content

Instantly share code, notes, and snippets.

Last active December 24, 2017 18:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pawlos/2b5bcb1e02839e66c3fed8c6bce57f29 to your computer and use it in GitHub Desktop.
Solution to Day 8: I Heard You Like Registers - Part 2
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 =
op =
val_to_mod = int(
reg_to_check =
op_to_check =
value_to_check = int(
#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