-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
196 lines (178 loc) · 3.16 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bufio"
"fmt"
lib "github.com/teivah/advent-of-code"
"io"
"math"
"strconv"
)
func fs1(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
var instructions []Instruction
reg := make(map[string]int)
for scanner.Scan() {
instruction := toInstruction(scanner.Text())
instructions = append(instructions, instruction)
reg[instruction.reg] = 0
}
for _, ins := range instructions {
action := 0
if ins.increment {
action += ins.value
} else {
action -= ins.value
}
a := reg[ins.cond.reg]
b := ins.cond.value
switch ins.cond.op {
case less:
if a < b {
reg[ins.reg] += action
}
case lessOrEqual:
if a <= b {
reg[ins.reg] += action
}
case greater:
if a > b {
reg[ins.reg] += action
}
case greaterOrEqual:
if a >= b {
reg[ins.reg] += action
}
case equal:
if a == b {
reg[ins.reg] += action
}
case different:
if a != b {
reg[ins.reg] += action
}
}
}
max := math.MinInt
for _, v := range reg {
max = lib.Max(max, v)
}
fmt.Println(reg)
return max, nil
}
func toInstruction(s string) Instruction {
spaces := lib.IndexAll(s, " ")
reg := s[:spaces[0]]
increment := true
if s[spaces[0]+1:spaces[1]] == "dec" {
increment = false
}
value, err := strconv.Atoi(s[spaces[1]+1 : spaces[2]])
if err != nil {
panic(err)
}
return Instruction{
reg: reg,
increment: increment,
value: value,
cond: toCondition(s[spaces[3]+1:]),
}
}
// "t != -1976"
func toCondition(s string) Condition {
spaces := lib.IndexAll(s, " ")
reg := s[:spaces[0]]
var op operator
switch s[spaces[0]+1 : spaces[1]] {
case "<":
op = less
case "<=":
op = lessOrEqual
case ">":
op = greater
case ">=":
op = greaterOrEqual
case "==":
op = equal
case "!=":
op = different
}
value, err := strconv.Atoi(s[spaces[1]+1:])
if err != nil {
panic(err)
}
return Condition{
reg: reg,
op: op,
value: value,
}
}
type Instruction struct {
reg string
increment bool
value int
cond Condition
}
type Condition struct {
reg string
op operator
value int
}
type operator int
const (
less operator = iota
lessOrEqual
greater
greaterOrEqual
equal
different
)
func fs2(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
var instructions []Instruction
reg := make(map[string]int)
for scanner.Scan() {
instruction := toInstruction(scanner.Text())
instructions = append(instructions, instruction)
reg[instruction.reg] = 0
}
max := math.MinInt
for _, ins := range instructions {
action := 0
if ins.increment {
action += ins.value
} else {
action -= ins.value
}
a := reg[ins.cond.reg]
b := ins.cond.value
switch ins.cond.op {
case less:
if a < b {
reg[ins.reg] += action
}
case lessOrEqual:
if a <= b {
reg[ins.reg] += action
}
case greater:
if a > b {
reg[ins.reg] += action
}
case greaterOrEqual:
if a >= b {
reg[ins.reg] += action
}
case equal:
if a == b {
reg[ins.reg] += action
}
case different:
if a != b {
reg[ins.reg] += action
}
}
max = lib.Max(max, reg[ins.reg])
}
fmt.Println(reg)
return max, nil
}