|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "github.com/markkovari/advent_of_code/aoc-go-common" |
| 8 | +) |
| 9 | + |
| 10 | +type Day08 struct{} |
| 11 | + |
| 12 | +func (d Day08) Part1(input string) string { |
| 13 | + return fmt.Sprintf("%d", solve(input, false)) |
| 14 | +} |
| 15 | + |
| 16 | +func (d Day08) Part2(input string) string { |
| 17 | + return fmt.Sprintf("%d", solve(input, true)) |
| 18 | +} |
| 19 | + |
| 20 | +func solve(input string, part2 bool) int { |
| 21 | + registers := make(map[string]int) |
| 22 | + maxSeen := 0 |
| 23 | + for _, line := range strings.Split(input, "\n") { |
| 24 | + if line == "" { continue } |
| 25 | + p := strings.Fields(line) |
| 26 | + reg, op, val, condReg, condOp, condVal := p[0], p[1], p[2], p[4], p[5], p[6] |
| 27 | + v, _ := strconv.Atoi(val) |
| 28 | + cv, _ := strconv.Atoi(condVal) |
| 29 | + |
| 30 | + cond := false |
| 31 | + rVal := registers[condReg] |
| 32 | + switch condOp { |
| 33 | + case ">": cond = rVal > cv |
| 34 | + case "<": cond = rVal < cv |
| 35 | + case ">=": cond = rVal >= cv |
| 36 | + case "<=": cond = rVal <= cv |
| 37 | + case "==": cond = rVal == cv |
| 38 | + case "!=": cond = rVal != cv |
| 39 | + } |
| 40 | + |
| 41 | + if cond { |
| 42 | + if op == "inc" { registers[reg] += v } else { registers[reg] -= v } |
| 43 | + for _, val := range registers { |
| 44 | + if val > maxSeen { maxSeen = val } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + if part2 { return maxSeen } |
| 49 | + maxVal := 0 |
| 50 | + for _, val := range registers { if val > maxVal { maxVal = val } } |
| 51 | + return maxVal |
| 52 | +} |
| 53 | + |
| 54 | +func main() { common.Run(2017, 8, Day08{}) } |
0 commit comments