Created
December 11, 2017 16:56
-
-
Save SimonDanisch/5e28555d1c15b3c7497c935a6dde92e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getop(op) | |
op == :inc && return + | |
op == :dec && return - | |
eval(Base, op) | |
end | |
function eval_instr(instr, vars) | |
lhs, op, rhs = split(instr, " ") | |
slhs = Symbol(lhs) | |
val1 = get!(vars, slhs, 0) | |
val2 = parse(Int, rhs) | |
opval = getop(Symbol(op)) | |
opval(val1, val2), slhs | |
end | |
function eval_program(program, vars) | |
maxi = typemin(Int) | |
for line in split(program, "\n") | |
isempty(line) && continue | |
instr, cond = strip.(split(line, "if")) | |
condval, lhs = eval_instr(cond, vars) | |
if condval | |
val, lhs = eval_instr(instr, vars) | |
maxi = max(maxi, val) | |
vars[lhs] = val | |
end | |
end | |
maxi | |
end | |
program = readstring("puzzle8.txt") | |
vars = Dict{Symbol, Int}() | |
currentmax = maximum(values(vars)) | |
maximax = eval_program(program, vars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment