-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20-2.go
55 lines (52 loc) · 895 Bytes
/
day20-2.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
package main
import (
"fmt"
"slices"
)
func day20part2(filename string) (string, error) {
f, err := day20ReadMod(filename)
if err != nil {
return "", err
}
st := day20State{
sys: f,
flip: map[string]bool{},
conj: map[string]map[string]int{},
}
var dirty = map[string]int{}
for _, v := range st.dirtyModules() {
dirty[v] = 0
}
var count int
for {
count += 1
st.pressButton(func(to string, val int) bool {
if _, ok := dirty[to]; ok && val == 0 {
dirty[to] = count
}
return false
})
var cyc int
for _, v := range dirty {
if v > 1 {
cyc += 1
}
}
if cyc == len(dirty) {
break
}
}
res := 1
for _, v := range dirty {
res *= v
}
return fmt.Sprint(res), nil
}
func (st day20State) dirtyModules() []string {
for _, v := range st.sys.mods {
if slices.Contains(v.output, "rx") {
return st.sys.inp[v.name]
}
}
return nil
}