Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created December 11, 2017 16:56
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 SimonDanisch/5e28555d1c15b3c7497c935a6dde92e7 to your computer and use it in GitHub Desktop.
Save SimonDanisch/5e28555d1c15b3c7497c935a6dde92e7 to your computer and use it in GitHub Desktop.
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