From 00e60657fa6f31a835f6a166242d74211716fd4d Mon Sep 17 00:00:00 2001 From: efim Date: Wed, 20 Dec 2023 09:45:52 +0000 Subject: [PATCH] day20, example 2 first four steps pass --- day20/modules.go | 4 +++ day20/propagation_test.go | 76 +++++++++++++++++++++++++++++++++++++++ day20/pulsePropagation.go | 51 ++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 day20/propagation_test.go diff --git a/day20/modules.go b/day20/modules.go index 06b02eb..65171e6 100644 --- a/day20/modules.go +++ b/day20/modules.go @@ -9,6 +9,10 @@ import ( ) type PulseType int +func (pt PulseType)String() string { + types := []string{"high", "low"} + return types[pt] +} const ( HighPulse PulseType = iota diff --git a/day20/propagation_test.go b/day20/propagation_test.go new file mode 100644 index 0000000..4a8baa4 --- /dev/null +++ b/day20/propagation_test.go @@ -0,0 +1,76 @@ +package day20 + +import ( + "testing" +) + +func TestPropagateButtonPressExample1(t *testing.T) { + filename := "example1" + modules := ReadModules(filename) + t.Log("got modules:\n", modules) + + low, high := PropagateButtonPress(modules) + t.Logf("got low %d and high %d\n", low, high) + t.Log("modules after single button press:\n", modules) + + success := low == 8 && high == 4 + if !success { + t.Errorf("expected low 8 got %d, high 4 got %d", low, high) + } +} + +func TestPropagateButtonPressExample2(t *testing.T) { + filename := "example2" + modules := ReadModules(filename) + t.Log("got modules:\n", modules) + InitStuffs(modules) + + low, high := PropagateButtonPress(modules) + t.Logf("got low %d and high %d\n", low, high) + t.Log("modules after single button press:\n", modules) + + success := low == 4 && high == 4 + if !success { + t.Errorf("expected low 4 got %d, high 4 got %d", low, high) + } +} + +func TestPropagateButtonPressExample2FourSteps(t *testing.T) { + filename := "example2" + modules := ReadModules(filename) + t.Log("got modules:\n", modules) + InitStuffs(modules) + + low, high := PropagateButtonPress(modules) + t.Logf("got low %d and high %d\n", low, high) + t.Log("#1 button press:\n", modules) + success := low == 4 && high == 4 + if !success { + t.Errorf("expected low 4 got %d, high 4 got %d", low, high) + } + + low, high = PropagateButtonPress(modules) + t.Logf("got low %d and high %d\n", low, high) + t.Log("#2 button press:\n", modules) + success = low == 4 && high == 2 + if !success { + t.Errorf("expected low 4 got %d, high 2 got %d", low, high) + } + + low, high = PropagateButtonPress(modules) + t.Logf("got low %d and high %d\n", low, high) + t.Log("#3 button press:\n", modules) + success = low == 5 && high == 3 + if !success { + t.Errorf("expected low 5 got %d, high 3 got %d", low, high) + } + + low, high = PropagateButtonPress(modules) + t.Logf("got low %d and high %d\n", low, high) + t.Log("#4 button press:\n", modules) + success = low == 4 && high == 2 + if !success { + t.Errorf("expected low 4 got %d, high 2 got %d", low, high) + } + +} diff --git a/day20/pulsePropagation.go b/day20/pulsePropagation.go index 6ebb715..7c7f85f 100644 --- a/day20/pulsePropagation.go +++ b/day20/pulsePropagation.go @@ -10,8 +10,50 @@ import ( func Run() int { fmt.Println("hello from dya 20") + filename := "day20/example2" + modules := ReadModules(filename) + log.Print("got modules:\n", modules) + + low, high := PropagateButtonPress(modules) + log.Printf("got low %d and high %d\n", low, high) + log.Print("modules after single button press:\n", modules) + return 0 } + +func PropagateButtonPress(modules map[string]Module) (lowSignalsCount, highSignalsCount int) { + signals := []Signal{{From: "button", To: "broadcast", PulseType: LowPulse}} + lowSignalsCount += 1 + + for len(signals) > 0 { + curSignal := signals[0] + signals = signals[1:] + + log.Printf("%s -%s-> %s", curSignal.From, curSignal.PulseType, curSignal.To) + + receivingModule, found := modules[curSignal.To] + if !found { + panic(fmt.Sprintf("signal %+v can't find it's recepient\n", curSignal)) + } + + newSignals := receivingModule.Receive(curSignal) + + // all newSignals will have same type + newSignalsAmount := len(newSignals) + if newSignalsAmount > 0 { + signals = append(signals, newSignals...) + someNewSignal := newSignals[0] + if someNewSignal.PulseType == HighPulse { + highSignalsCount += newSignalsAmount + } else { + lowSignalsCount += newSignalsAmount + } + } + } + + return +} + // process sends single `low pulse` directly to "broadcast" func ReadModules(filename string) map[string]Module { @@ -46,3 +88,12 @@ func ReadModules(filename string) map[string]Module { return result } + +func InitStuffs(allModules map[string]Module) { + for _, module := range allModules { + if conjunction, ok := module.(*Conjunction); ok { + conjunction.RegisterInputs(allModules) + } + } + +}