-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.go
197 lines (172 loc) · 4.55 KB
/
day3.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
package main
type Day3 struct {
filePath string
}
func (Day3) Name() string {
return "day3"
}
func (day Day3) FilePath() string {
return day.filePath
}
// lol... there's no built-in abs function for integers - only floats.
// I assume lack of generics was the original reason why?? jeez.
// Abs returns the absolute value of x.
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
func isDigit(char byte) bool {
return char >= byte('0') && char <= byte('9')
}
type PartNumber struct {
startIndex int
endIndex int
num int
visited bool
}
func (p PartNumber) IsAdjacent(i int) bool {
return Abs(i-p.startIndex) <= 1 || Abs(i-p.endIndex) <= 1
}
type Symbol struct {
lineIndex int
colIndex int
symbol byte
}
func parse(lines []string) ([][]PartNumber, []Symbol) {
// Represents all numbers given a line index. Sorted by `startIndex` and `endIndex`.
lineToPartNums := make([][]PartNumber, len(lines))
symbols := make([]Symbol, 0)
// Unicode makes the parsing code more complicated (can't manually increment iterator...), so
// I'm just going to iterate over the bytes instead.
for i, line := range lines {
for j := 0; j < len(line); j++ {
char := line[j]
// garbage
if char == byte('.') {
continue
}
// symbol
if !isDigit(char) {
symbols = append(symbols, Symbol{
lineIndex: i,
colIndex: j,
symbol: char,
})
continue
}
// digit
startIndex := j
num := 0
for isDigit(char) {
num *= 10
num += int(char - byte('0'))
j++
if j >= len(line) {
break
}
char = line[j]
}
// current character isn't a digit. let's rewind...
j--
endIndex := j
part := PartNumber{
startIndex: startIndex,
endIndex: endIndex,
num: num,
visited: false,
}
lineToPartNums[i] = append(lineToPartNums[i], part)
}
}
return lineToPartNums, symbols
}
// two-pass algorithm:
// 1. parse all numbers and symbols into a sane, searchable format.
// 2. visit all symbols and determine which numbers "belong" to them.
func (Day3) Part1(lines []string) (int, error) {
lineToPartNums, symbols := parse(lines)
total := 0
for _, symbol := range symbols {
checkLine := func(s Symbol, line []PartNumber) {
// yes, binary search here for finding part numbers (aka "intervals") given an index is best
// here, but given that the input lines are relatively short, it's probably slower than this
// cache-efficient brute-force approach.
//
// more generally, using an interval tree can work.
for i := 0; i < len(line); i++ {
partNum := &line[i]
if partNum.visited || !partNum.IsAdjacent(symbol.colIndex) {
continue
}
total += partNum.num
partNum.visited = true
}
}
// check symbol's line
checkLine(symbol, lineToPartNums[symbol.lineIndex])
// check above
if symbol.lineIndex > 0 {
checkLine(symbol, lineToPartNums[symbol.lineIndex-1])
}
// check below
if symbol.lineIndex < len(lines)-1 {
checkLine(symbol, lineToPartNums[symbol.lineIndex+1])
}
}
return total, nil
}
// two-pass algorithm:
// 1. parse all numbers and symbols into a sane, searchable format.
// 2. visit all "gears" and determine whether they have exactly 2 numbers.
func (Day3) Part2(lines []string) (int, error) {
lineToPartNums, symbols := parse(lines)
gearNumbers := make([]*PartNumber, 2)
total := 0
for _, symbol := range symbols {
if symbol.symbol != byte('*') {
continue
}
numParts := 0
exceedsLimit := false
checkLine := func(s Symbol, line []PartNumber) {
// yes, binary search here for finding part numbers (aka "intervals") given an index is best
// here, but given that the input lines are relatively short, it's probably slower than this
// cache-efficient brute-force approach.
//
// more generally, using an interval tree can work.
if exceedsLimit {
return
}
for i := 0; i < len(line); i++ {
partNum := &line[i]
if partNum.visited || !partNum.IsAdjacent(symbol.colIndex) {
continue
}
partNum.visited = true
if numParts >= 2 {
exceedsLimit = true
return
}
gearNumbers[numParts] = partNum
numParts++
}
}
// check symbol's line
checkLine(symbol, lineToPartNums[symbol.lineIndex])
// check above
if symbol.lineIndex > 0 {
checkLine(symbol, lineToPartNums[symbol.lineIndex-1])
}
// check below
if symbol.lineIndex < len(lines)-1 {
checkLine(symbol, lineToPartNums[symbol.lineIndex+1])
}
if !exceedsLimit && numParts == 2 {
total += gearNumbers[0].num * gearNumbers[1].num
}
clear(gearNumbers)
}
return total, nil
}