-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6part2.go
76 lines (61 loc) · 1.14 KB
/
day6part2.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
package main
import (
"fmt"
"io"
"os"
)
// Now you're thinking with structs!
// going to hard-code an array just so it works faster, I guess?
type SigBuffer struct {
buffer [14]byte
}
func (s *SigBuffer) push(char byte) {
for i := 13; i > 0; i-- {
s.buffer[i] = s.buffer[i-1]
}
s.buffer[0] = char
}
func (s *SigBuffer) allValuesFilled() bool {
// returns true if all items in buffer contain data
for _, v := range s.buffer {
if v == 0 {
return false
}
}
return true
}
func (s *SigBuffer) newSignal() bool {
// Will check to see if a new pattern exists
if !s.allValuesFilled() {
return false
}
for i := 0; i < len(s.buffer); i++ {
for j := i + 1; j < len(s.buffer); j++ {
if s.buffer[i] == s.buffer[j] {
return false
}
}
}
return true
}
func main() {
readFile, err := os.Open("input")
if err != nil {
fmt.Println(err)
}
defer readFile.Close()
content, err := io.ReadAll(readFile)
if err != nil {
fmt.Println("Fatal:", err)
}
s := SigBuffer{}
counter := 1
for _, v := range content {
s.push(v)
if s.newSignal() {
break
}
counter++
}
fmt.Printf("Signal digit count: %d\n", counter)
}