-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (74 loc) · 1.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Package day08 - Solution for Advent of Code 2017/08
// Problem Link: http://adventofcode.com/2017/day/08
package day08
import (
_ "embed"
"fmt"
"github.com/code-shoily/aocgo/seq"
"golang.org/x/exp/maps"
"math"
"strings"
)
//go:embed input.txt
var input string
// Run prints out the result of the solution.
func Run() {
fmt.Println(solve(input))
}
func solve(input string) (int, int) {
instructions := parse(input)
registers := map[string]int{}
largestEver := math.MinInt // Part 2
for _, i := range instructions {
if i.compareFunc(registers) {
if updatedValue := i.updateFunc(registers); updatedValue > largestEver {
largestEver = updatedValue
}
}
}
_, max := seq.GetMinMax(maps.Values(registers)) // Part 1
return max, largestEver
}
func parse(input string) (instructions []instruction) {
for _, line := range strings.Split(input, "\n") {
var reg, updater, dep, cmp string
var updateVal, cmpVal int
fmt.Sscanf(line, "%s %s %d if %s %s %d",
®, &updater, &updateVal, &dep, &cmp, &cmpVal)
instructions = append(instructions, newInstruction(reg, updater, updateVal, dep, cmp, cmpVal))
}
return instructions
}
type instruction struct {
updateFunc func(map[string]int) int
compareFunc func(map[string]int) bool
}
func newInstruction(reg string, updater string, updateVal int, dep string, cmp string, cmpVal int) instruction {
updateFunc := func(registers map[string]int) int {
switch updater {
case "inc":
registers[reg] += updateVal
case "dec":
registers[reg] -= updateVal
}
return registers[reg]
}
compareFunc := func(registers map[string]int) bool {
switch cmp {
case "==":
return registers[dep] == cmpVal
case "!=":
return registers[dep] != cmpVal
case "<":
return registers[dep] < cmpVal
case "<=":
return registers[dep] <= cmpVal
case ">":
return registers[dep] > cmpVal
case ">=":
return registers[dep] >= cmpVal
}
panic("Logical Error: Unreachable Code")
}
return instruction{updateFunc, compareFunc}
}