-
Notifications
You must be signed in to change notification settings - Fork 1
/
aoc6.go
85 lines (74 loc) · 1.81 KB
/
aoc6.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
// aoc6.go --
// advent of code 2022 day 6
//
// https://adventofcode.com/2022/day/6
// https://github.com/erik-adelbert/aoc
//
// (ɔ) Erik Adelbert - erik_AT_adelbert_DOT_fr
// -------------------------------------------
// 2022-12-6: initial commit
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// state handlers for parts 1&2
// https://go.dev/talks/2011/lex.slide
type state func(int) (next state, match bool)
var (
wlen int
seen [128]int
part1, part2 state
)
// https://go.dev/talks/2011/lex.slide#19
part1 = func(wlen int) (next state, match bool) {
if wlen == 4 {
return part2, true // part1 done, transition to part2
}
return part1, false // part1 not done, no match
}
part2 = func(wlen int) (next state, match bool) {
if wlen == 14 {
return nil, true // part2 done, transition to end
}
return part2, false // part2 not done, no match
}
// initial state is solving for part1
check := part1
// slide over single line input
input := bufio.NewScanner(os.Stdin)
input.Scan()
for i, c := range input.Bytes() {
// outside current window?
// extend window!
// or
// repeating inside?
// shrink window!
switch {
case i-seen[c] > wlen:
wlen++ // extend right
case i-seen[c] < wlen:
wlen = i - seen[c] // shrink left
}
seen[c] = i
// dynamic state machine
// states are self transitioning check functions
var match bool
if check, match = check(wlen); match {
fmt.Println(i + 1)
if check == nil { // terminal state
return
}
}
// terminal state check could be here (canonical) but
// in this case it would only be an extraneous test as
// we already know that transitions occurs on matches
// see https://go.dev/talks/2011/lex.slide#20
//
// if check == nil { // terminal state
// return
// }
}
}