day20: whelp.

This commit is contained in:
efim
2023-12-20 11:14:45 +00:00
parent 1e32ec0988
commit 57fdfb01cb
6 changed files with 272 additions and 17 deletions

View File

@@ -8,16 +8,19 @@ import (
)
func Run() int {
fmt.Println("hello from dya 20")
// fmt.Println("hello from dya 20")
filename := "day20/input"
modules := ReadModules(filename)
InitStuffs(modules)
log.Print("got modules:\n", modules)
low, high := Count10000ButtonPresses(modules)
var low, high int
// low, high = Count10000ButtonPresses(modules)
log.Printf("got low %d and high %d\n", low, high)
fmt.Println(AllMermaidFlowChard(modules))
return low * high
}
@@ -35,10 +38,13 @@ func Count10000ButtonPresses(modules map[string]Module) (lowSignalsCount, highSi
// for now let's just print the info on loop
for i := 0; i < count; i++ {
stepLow, stepHigh := PropagateButtonPress(modules)
if i % 10000 == 0 {
log.Println("done button presses: ", i)
}
stepLow, stepHigh := PropagateButtonPress(modules, i)
lowSignalsCount += stepLow
highSignalsCount += stepHigh
log.Printf("after step %d low is %d and high is %d", i, lowSignalsCount, highSignalsCount)
// log.Printf("after step %d low is %d and high is %d", i, lowSignalsCount, highSignalsCount)
state := ModulesState(modules)
prevCounts, found := countsAfterState[state]
@@ -65,7 +71,7 @@ func Count10000ButtonPresses(modules map[string]Module) (lowSignalsCount, highSi
return
}
func PropagateButtonPress(modules map[string]Module) (lowSignalsCount, highSignalsCount int) {
func PropagateButtonPress(modules map[string]Module, i int) (lowSignalsCount, highSignalsCount int) {
signals := []Signal{{From: "button", To: "broadcast", PulseType: LowPulse}}
lowSignalsCount += 1
@@ -73,11 +79,14 @@ func PropagateButtonPress(modules map[string]Module) (lowSignalsCount, highSigna
curSignal := signals[0]
signals = signals[1:]
log.Printf("%s -%s-> %s", curSignal.From, curSignal.PulseType, curSignal.To)
// log.Printf("%s -%s-> %s", curSignal.From, curSignal.PulseType, curSignal.To)
receivingModule, found := modules[curSignal.To]
if !found {
log.Print(fmt.Sprintf("signal %+v can't find it's recepient\n", curSignal))
// log.Print(fmt.Sprintf("signal %+v can't find it's recepient\n", curSignal))
if curSignal.To == "rx" && curSignal.PulseType == LowPulse {
panic(fmt.Sprintf("getting low signal to rx, on step %d", i))
}
continue
}
@@ -153,3 +162,12 @@ func ModulesState(allModules map[string]Module) string {
return fmt.Sprint(states)
}
func AllMermaidFlowChard(allModules map[string]Module) (result string) {
result = "flowchart LR\n"
for _, module := range allModules {
result += module.MermaidFlow()
}
return
}