-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.go
239 lines (199 loc) · 4.44 KB
/
exercise.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package exercises
import (
"fmt"
"strconv"
"strings"
"github.com/asphaltbuffet/advent-of-code/internal/common"
)
// Exercise for Advent of Code 2022 day 10.
type Exercise struct {
common.BaseExercise
}
// One returns the answer to the first part of the exercise.
// answer: 17380
func (c Exercise) One(instr string) (any, error) {
data := strings.Split(instr, "\n")
part1 := Day10{
Cycle: 1,
X: map[int]int{0: 0, 1: 1},
Commands: []Command{},
Cycle20: 1,
Cycle60: 1,
Cycle100: 1,
Cycle140: 1,
Cycle180: 1,
Cycle220: 1,
}
err := part1.Parse(data)
if err != nil {
return nil, fmt.Errorf("parsing input: %w", err)
}
err = part1.Process()
if err != nil {
return nil, fmt.Errorf("processing input: %w", err)
}
calc := part1.Calculate()
return calc, nil
}
// Two returns the answer to the second part of the exercise.
// answer: FGCUZREC
func (c Exercise) Two(instr string) (any, error) {
data := strings.Split(instr, "\n")
part1 := Day10{
Cycle: 1,
X: map[int]int{0: 0, 1: 1},
Commands: []Command{},
Cycle20: 1,
Cycle60: 1,
Cycle100: 1,
Cycle140: 1,
Cycle180: 1,
Cycle220: 1,
}
err := part1.Parse(data)
if err != nil {
return nil, fmt.Errorf("parsing input: %w", err)
}
err = part1.Process()
if err != nil {
return nil, fmt.Errorf("processing input: %w", err)
}
return part1.Draw(), nil
}
// Instruction is string type for instructions.
type Instruction string
// Instruction constants.
const (
Add Instruction = "addx"
Noop Instruction = "noop"
)
// Cycle constants.
const (
Cycle20 int = 20
Cycle60 int = 60
Cycle100 int = 100
Cycle140 int = 140
Cycle180 int = 180
Cycle220 int = 220
)
// Command is a single instruction with a value.
type Command struct {
Instruction Instruction
Value int
}
// Day10 is the exercise environment.
type Day10 struct {
Cycle int
X map[int]int
Commands []Command
Cycle20 int
Cycle60 int
Cycle100 int
Cycle140 int
Cycle180 int
Cycle220 int
}
// Parse converts the input data into a tokenized form.
func (d *Day10) Parse(data []string) error {
d.Commands = []Command{}
for i, line := range data {
left, right, _ := strings.Cut(line, " ")
switch left {
case string(Add):
val, err := strconv.Atoi(right)
if err != nil {
return fmt.Errorf("parsing value on line %d: %w", i, err)
}
d.Commands = append(d.Commands, Command{Add, val})
case string(Noop):
d.Commands = append(d.Commands, Command{Noop, 0})
default:
return fmt.Errorf("unknown instruction on line %d: %s", i, left)
}
}
// log.Printf("commands: %v", d.Commands)
return nil
}
// Process executes the commands.
func (d *Day10) Process() error {
for i, cmd := range d.Commands {
switch {
case d.Cycle+2 <= Cycle20:
d.Cycle20 += cmd.Value
fallthrough
case d.Cycle+2 <= Cycle60:
d.Cycle60 += cmd.Value
fallthrough
case d.Cycle+2 <= Cycle100:
d.Cycle100 += cmd.Value
fallthrough
case d.Cycle+2 <= Cycle140:
d.Cycle140 += cmd.Value
fallthrough
case d.Cycle+2 <= Cycle180:
d.Cycle180 += cmd.Value
fallthrough
case d.Cycle+2 <= Cycle220:
d.Cycle220 += cmd.Value
}
switch cmd.Instruction {
case Add:
d.X[d.Cycle+2] = cmd.Value
d.Cycle += 2
case Noop:
d.Cycle++
default:
return fmt.Errorf("processing command %d: %+v", i, cmd)
}
}
// log.Printf("temp X: %+v", d.X)
// for i := 0; i < d.Cycle; i++ {
// d.X[i+1] += d.X[i]
// }
// log.Printf("final X: %v", d.X)
// log.Printf("cycle 20: %d", d.Cycle20)
// log.Printf("cycle 60: %d", d.Cycle60)
// log.Printf("cycle 100: %d", d.Cycle100)
// log.Printf("cycle 140: %d", d.Cycle140)
// log.Printf("cycle 180: %d", d.Cycle180)
// log.Printf("cycle 220: %d", d.Cycle220)
return nil
}
// Calculate returns the result of the processing.
func (d *Day10) Calculate() int {
return (Cycle20 * d.Cycle20) +
(Cycle60 * d.Cycle60) +
(Cycle100 * d.Cycle100) +
(Cycle140 * d.Cycle140) +
(Cycle180 * d.Cycle180) +
(Cycle220 * d.Cycle220)
}
// Draw returns a string showing which pixels are lit.
func (d *Day10) Draw() string {
const (
Lit string = "█"
Dark string = "░"
)
sb := strings.Builder{}
sb.WriteString("\n") // start on a new line
x := 1
for c := 1; c <= 240; c++ {
hpos := c % 40
x += d.X[c]
if abs(x-hpos) <= 1 {
sb.WriteString(Lit)
} else {
sb.WriteString(Dark)
}
if hpos == 0 {
sb.WriteString("\n")
}
}
return sb.String()
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}