In [1]:
example = [
"b inc 5 if a > 1"
"a inc 1 if b < 5"
"c dec -10 if a >= 1"
"c inc -20 if c == 10"
]
Out[1]:
In [19]:
using DataStructures
registers = DefaultDict(0)
function execute(program)
empty!(registers)
m = 0
for line in program
r1, op1, v1, r2, op2, v2 = match(r"([a-z]+) (inc|dec) ([-]?[0-9]+) if ([a-z]+) ([><=!]+) ([-]?[0-9]+)", line).captures
eval(parse("if registers[\""*r2*"\"] "*op2*" "*v2*"; registers[\""*r1*"\"] "*(op1 == "inc" ? "+=" : "-=")*" "*v1*" end"))
if length(registers)>0
m = max(m, maximum(values(registers)))
end
end
m
end
Out[19]:
In [21]:
println(" Maximum alltimes ", execute(example))
println("Maximum at the end ", maximum(values(registers)))
In [23]:
open("inputs/day8.txt") do fd
println(" Maximum alltimes ", execute(readlines(fd)))
println("Maximum at the end ", maximum(values(registers)))
end
In [ ]: