Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created December 8, 2017 13:34
Show Gist options
  • Save Dierk/61cc08559fbabc1a220cb6702de94203 to your computer and use it in GitHub Desktop.
Save Dierk/61cc08559fbabc1a220cb6702de94203 to your computer and use it in GitHub Desktop.
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