-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
175 lines (144 loc) · 3.68 KB
/
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"flag"
"fmt"
"regexp"
"strings"
"github.com/itsluketwist/advent-of-code-2023/utils"
)
const Day = 20
func main() {
part := flag.Int("part", 0, "Which parts to try?")
try := flag.Int("try", 0, "Whether to try the real input?")
flag.Parse()
fmt.Println("Running day", Day, "( part:", *part, ", try:", *try, ")")
exampleOne, _ := utils.ReadFileToArray(Day, "example1", false)
input, _ := utils.ReadFileToArray(Day, "input", false)
if *part == 0 || *part == 1 {
solutionOneExample := SolvePartOne(exampleOne)
fmt.Println("Solution to part 1 (example):", solutionOneExample)
if *try == 1 {
SolutionOneInput := SolvePartOne(input)
fmt.Println("Solution to part 1 (input):", SolutionOneInput)
}
}
if *part == 0 || *part == 2 {
if *try == 1 {
SolutionTwoInput := SolvePartTwo(input)
fmt.Println("Solution to part 2 (input):", SolutionTwoInput)
}
}
}
type FlipFlop struct {
on bool
targets []string
}
type Conjunction struct {
memory map[string]bool
targets []string
}
func parseData(data []string) ([]string, map[string]FlipFlop, map[string]Conjunction) {
flipFlops := make(map[string]FlipFlop)
conjunctions := make(map[string]Conjunction)
var broadcast []string
re := regexp.MustCompile("^([%&]?)(\\w+) -> (.*)$")
for _, line := range data {
match := re.FindSubmatch([]byte(line))
label := string(match[2])
targets := strings.Split(string(match[3]), ", ")
if label == "broadcaster" {
broadcast = targets
} else if string(match[1]) == "%" {
flipFlops[label] = FlipFlop{false, targets}
} else {
conjunctions[label] = Conjunction{make(map[string]bool), targets}
}
}
for label, flip := range flipFlops {
for _, target := range flip.targets {
if _, ok := conjunctions[target]; ok {
conjunctions[target].memory[label] = false
}
}
}
for label, con := range conjunctions {
for _, target := range con.targets {
if _, ok := conjunctions[target]; ok {
conjunctions[target].memory[label] = false
}
}
}
return broadcast, flipFlops, conjunctions
}
type Pulse struct {
target string
trigger string
high bool
}
func Solve(data []string, part int) int {
highCount, lowCount := 0, 0
broadcast, flipFlops, conjunctions := parseData(data)
var loopMax int
if part == 1 {
loopMax = 1000
} else {
loopMax = 10000
}
queue := utils.Queue[Pulse]{}
var partTwoNums []int
pressed := map[string]bool{
"kf": false, "kr": false, "qk": false, "zs": false,
}
for i := 0; i < loopMax; i++ {
if len(partTwoNums) == 4 {
return utils.LCM(partTwoNums)
}
lowCount++ // button pressed
for _, target := range broadcast {
queue.Push(Pulse{target, "broadcast", false})
}
for {
if queue.Len() == 0 {
break
}
pulse := queue.Pop()
if pulse.high {
highCount++
} else {
lowCount++
}
for key, val := range pressed {
if !val && pulse.target == "gf" && conjunctions["gf"].memory[key] {
pressed[key] = true
partTwoNums = append(partTwoNums, i+1)
}
}
if flip, ok := flipFlops[pulse.target]; ok {
if !pulse.high {
flip.on = !flip.on
for _, t := range flip.targets {
queue.Push(Pulse{t, pulse.target, flip.on})
}
flipFlops[pulse.target] = flip
}
} else if con, ok := conjunctions[pulse.target]; ok {
con.memory[pulse.trigger] = pulse.high
allHigh := true
for _, v := range con.memory {
allHigh = allHigh && v
}
for _, t := range con.targets {
queue.Push(Pulse{t, pulse.target, !allHigh})
}
conjunctions[pulse.target] = con
}
}
}
return highCount * lowCount
}
func SolvePartOne(data []string) int {
return Solve(data, 1)
}
func SolvePartTwo(data []string) int {
return Solve(data, 2)
}