-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.go
81 lines (63 loc) · 1.2 KB
/
day03.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
package main
import (
"io"
"github.com/armsnyder/aoc2022/aocutil"
)
var _ = declareDay(3, func(part2 bool, inputReader io.Reader) any {
if part2 {
return day03Part2(inputReader)
}
return day03Part1(inputReader)
})
func day03Part1(inputReader io.Reader) any {
var cache [26*2 + 1]bool
total := 0
aocutil.VisitStrings(inputReader, func(v []byte) {
for _, b := range v[:len(v)/2] {
p := day03Priority(b)
cache[p] = true
}
for _, b := range v[len(v)/2:] {
p := day03Priority(b)
if cache[p] {
total += p
break
}
}
for i := range cache {
cache[i] = false
}
})
return total
}
func day03Part2(inputReader io.Reader) any {
var cache [26*2 + 1]int
total := 0
lineIndex := 0
aocutil.VisitStrings(inputReader, func(v []byte) {
defer func() {
lineIndex++
}()
elfIndex := lineIndex % 3
for _, b := range v {
p := day03Priority(b)
if cache[p] == elfIndex {
if elfIndex == 2 {
total += p
for i := range cache {
cache[i] = 0
}
return
}
cache[p] = elfIndex + 1
}
}
})
return total
}
func day03Priority(item byte) int {
if item >= 'a' {
return int(item) - int('a') + 1
}
return int(item) - int('A') + 26 + 1
}