Created
December 8, 2017 13:34
-
-
Save Dierk/61cc08559fbabc1a220cb6702de94203 to your computer and use it in GitHub Desktop.
Advent of Groovy code, day 8
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
// 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