day20, part2 done with online LCM calculator

This commit is contained in:
efim
2023-12-20 13:16:13 +00:00
parent 57fdfb01cb
commit 98206fe6d4
4 changed files with 318 additions and 13 deletions

View File

@@ -13,15 +13,39 @@ func Run() int {
filename := "day20/input"
modules := ReadModules(filename)
InitStuffs(modules)
log.Print("got modules:\n", modules)
// log.Print("got modules:\n", modules)
var low, high int
// var low, high int
// low, high = Count10000ButtonPresses(modules)
log.Printf("got low %d and high %d\n", low, high)
// log.Printf("got low %d and high %d\n", low, high)
fmt.Println(AllMermaidFlowChard(modules))
CheckSubgraphsStuff(modules)
return low * high
var result int
// result = CalcCommonStep()
return result
}
func CheckSubgraphsStuff(allModules map[string]Module) {
// loopStarts := allModules["broadcast"].Outputs()
// loop start and loop sink
loopItems := map[string]string {
// "sr": "xn",
// "ch": "xf",
// "hd": "qn",
"bx": "zl",
}
for start, end := range loopItems {
log.Printf(">>> searching for loop of %s", start)
themap := make(map[string]any)
loopModules := TransitiveOutputs(start, allModules, themap)
_, _, requestedPulses := FindSubGraphLoopLength(loopModules, allModules, end)
FilterMonitoredPulses(requestedPulses)
log.Printf("the pulses: +%v", requestedPulses)
}
}
func Count10000ButtonPresses(modules map[string]Module) (lowSignalsCount, highSignalsCount int) {
@@ -108,6 +132,41 @@ func PropagateButtonPress(modules map[string]Module, i int) (lowSignalsCount, hi
return
}
func PropagateButtonPressWithMonitor(modules map[string]Module, i int, monitorAllOutputsOf string) []PulseType {
result := make([]PulseType, 0)
signals := []Signal{{From: "button", To: "broadcast", PulseType: LowPulse}}
for len(signals) > 0 {
curSignal := signals[0]
signals = signals[1:]
if curSignal.From == monitorAllOutputsOf {
result = append(result, curSignal.PulseType)
}
// 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))
if curSignal.To == "rx" && curSignal.PulseType == LowPulse {
panic(fmt.Sprintf("getting low signal to rx, on step %d", i))
}
continue
}
newSignals := receivingModule.Receive(curSignal)
// all newSignals will have same type
newSignalsAmount := len(newSignals)
if newSignalsAmount > 0 {
signals = append(signals, newSignals...)
}
}
return result
}
// process sends single `low pulse` directly to "broadcast"
func ReadModules(filename string) map[string]Module {
@@ -132,7 +191,7 @@ func ReadModules(filename string) map[string]Module {
result[parsed.Name] = &parsed
}
log.Println(line)
// log.Println(line)
}
buttonModule := Button{}
@@ -152,11 +211,11 @@ func InitStuffs(allModules map[string]Module) {
}
func ModulesState(allModules map[string]Module) string {
func ModulesState(modules map[string]Module) string {
// relying on printing of map values to be ordered by key
// https://stackoverflow.com/a/54524991/2788805
states := make(map[string]string)
for name, module := range allModules {
for name, module := range modules {
states[name] = module.StateSnapshot()
}
@@ -164,7 +223,7 @@ func ModulesState(allModules map[string]Module) string {
}
func AllMermaidFlowChard(allModules map[string]Module) (result string) {
result = "flowchart LR\n"
result = "flowchart TD\n"
for _, module := range allModules {
result += module.MermaidFlow()
}