Create a gist now

Instantly share code, notes, and snippets.

Embed
Advent of Groovy code, day 8
// http://adventofcode.com/2017/day/8
def input = '''b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10'''
regs =[:].withDefault{0}
inc = { String key, Integer x -> regs[key] += x }
dec = { String key, Integer x -> inc(key, -x) }
input.readLines().each {
def match = it =~ /^(\w+) (inc|dec) (-?\d+) if (.*)/
def (full, reg, op, arg, cond) = match[0]
def code = "if (regs.$cond) $op('$reg',$arg)"
evaluate code
}
println "Largest register value: " + regs.values().max()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment