-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (37 loc) · 763 Bytes
/
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
package main
import (
"fmt"
utils "github.com/baspar/adventofcode2022/internal"
)
type DayImpl struct {
stream string
}
func (d *DayImpl) findRollingWindow(size int) int {
hist := make(map[byte]int)
for i, upper := range d.stream {
if i >= size {
lower := d.stream[i-size]
if hist[lower]--; hist[lower] == 0 {
delete(hist, lower)
}
}
hist[byte(upper)]++
if len(hist) == size {
return i + 1
}
}
return -1
}
func (d *DayImpl) Init(lines []string) error {
d.stream = lines[0]
return nil
}
func (d *DayImpl) Part1() (string, error) {
return fmt.Sprint(d.findRollingWindow(4)), nil
}
func (d *DayImpl) Part2() (string, error) {
return fmt.Sprint(d.findRollingWindow(14)), nil
}
func main() {
utils.Run(&DayImpl{}, false)
}