Skip to content

Instantly share code, notes, and snippets.

@pawlos
Last active December 24, 2017 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawlos/6aeb8a524e94cb83b07cb9a799711cd4 to your computer and use it in GitHub Desktop.
Save pawlos/6aeb8a524e94cb83b07cb9a799711cd4 to your computer and use it in GitHub Desktop.
Solution to Day 8: I Heard You Like Registers - part 1
#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()
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)
#print regs
#print regs
r = max(regs, key=lambda i: regs[i])
print r,"=",regs[r]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment