-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8_part1.go
111 lines (96 loc) · 2.07 KB
/
day8_part1.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package day8_part1
import (
"fmt"
"slices"
"strings"
"GoAdventOfCode/util"
)
type Registers map[string]int
func getAnswer(lines []string) int {
registers := make(Registers)
for _, line := range lines {
// 0 1 2 3 4 5 6
// [r1] [inc|dec] [v1] if [r2] [cmp] [v2]
parts := strings.Fields(line)
r1 := parts[0]
r2 := parts[4]
cmd := parts[1]
cmp := parts[5]
v1 := util.ConvertStringToInt(parts[2])
v2 := util.ConvertStringToInt(parts[6])
if _, ok := registers[r1]; !ok {
registers[r1] = 0
}
if _, ok := registers[r2]; !ok {
registers[r2] = 0
}
switch {
case cmd == "inc" && cmp == "==":
if registers[r2] == v2 {
registers[r1] += v1
}
case cmd == "inc" && cmp == "!=":
if registers[r2] != v2 {
registers[r1] += v1
}
case cmd == "inc" && cmp == ">":
if registers[r2] > v2 {
registers[r1] += v1
}
case cmd == "inc" && cmp == ">=":
if registers[r2] >= v2 {
registers[r1] += v1
}
case cmd == "inc" && cmp == "<":
if registers[r2] < v2 {
registers[r1] += v1
}
case cmd == "inc" && cmp == "<=":
if registers[r2] <= v2 {
registers[r1] += v1
}
case cmd == "dec" && cmp == "==":
if registers[r2] == v2 {
registers[r1] -= v1
}
case cmd == "dec" && cmp == "!=":
if registers[r2] != v2 {
registers[r1] -= v1
}
case cmd == "dec" && cmp == "<":
if registers[r2] < v2 {
registers[r1] -= v1
}
case cmd == "dec" && cmp == "<=":
if registers[r2] <= v2 {
registers[r1] -= v1
}
case cmd == "dec" && cmp == ">":
if registers[r2] > v2 {
registers[r1] -= v1
}
case cmd == "dec" && cmp == ">=":
if registers[r2] >= v2 {
registers[r1] -= v1
}
}
}
maxValue := 0
//printRegisters(registers)
for _, v := range registers {
if v > maxValue {
maxValue = v
}
}
return maxValue
}
func printRegisters(registers Registers) {
keys := make([]string, 0, len(registers))
for k := range registers {
keys = append(keys, k)
}
slices.Sort(keys)
for _, v := range keys {
fmt.Printf("%v => \"%+v\"\n", v, registers[v])
}
}