From f538945dff57750d08551cb540919ab2c3598b19 Mon Sep 17 00:00:00 2001 From: efim Date: Wed, 20 Dec 2023 09:58:40 +0000 Subject: [PATCH] day20, modules state comparations --- day20/modules.go | 20 ++++++++++++++++++++ day20/propagation_test.go | 19 +++++++++++++++++++ day20/pulsePropagation.go | 12 ++++++++++++ 3 files changed, 51 insertions(+) diff --git a/day20/modules.go b/day20/modules.go index 65171e6..ede91a6 100644 --- a/day20/modules.go +++ b/day20/modules.go @@ -27,6 +27,7 @@ type Signal struct { type Module interface { Receive(s Signal) []Signal Outputs() []string + StateSnapshot() string } // Modules @@ -71,6 +72,10 @@ func (ff *FlipFlop)String() string { return fmt.Sprintf("[flip-flop '%s' (on: %t) -> %s]", ff.Name, ff.IsOn, ff.OutputNames) } +func (ff *FlipFlop)StateSnapshot() string { + return ff.String() +} + func IsLineFlipFlop(line string) bool { return strings.HasPrefix(line, "%") } @@ -108,6 +113,9 @@ func (b *Broadcast)Outputs() []string { func (b *Broadcast)String() string { return fmt.Sprintf("[broadcast -> %+v]", b.OutputNames) } +func (b *Broadcast)StateSnapshot() string { + return b.String() +} func IsLineBroadcast(line string) bool { return strings.HasPrefix(line, "broadcaster") @@ -174,6 +182,10 @@ func (c *Conjunction)String() string { return fmt.Sprintf("[conjunction '%s' -> %+v]", c.Name, c.OutputNames) } +func (c *Conjunction)StateSnapshot() string { + return fmt.Sprintf("[conjunction '%s' -> %+v]", c.Name, c.MostRecentPulseFromInputIsHigh) +} + func IsLineConjunction(line string) bool { return strings.HasPrefix(line, "&") } @@ -207,6 +219,10 @@ func (b *Button)String() string { return "[button]" } +func (b *Button)StateSnapshot() string { + return b.String() +} + type Output struct {} func (o *Output)Receive(s Signal) []Signal { @@ -221,3 +237,7 @@ func (o *Output)Outputs() []string { func (o *Output)String() string { return "[output]" } + +func (o *Output)StateSnapshot() string { + return o.String() +} diff --git a/day20/propagation_test.go b/day20/propagation_test.go index 4a8baa4..f10c952 100644 --- a/day20/propagation_test.go +++ b/day20/propagation_test.go @@ -1,6 +1,7 @@ package day20 import ( + "log" "testing" ) @@ -41,6 +42,8 @@ func TestPropagateButtonPressExample2FourSteps(t *testing.T) { t.Log("got modules:\n", modules) InitStuffs(modules) + initialModulesState := ModulesState(modules) + low, high := PropagateButtonPress(modules) t.Logf("got low %d and high %d\n", low, high) t.Log("#1 button press:\n", modules) @@ -56,6 +59,10 @@ func TestPropagateButtonPressExample2FourSteps(t *testing.T) { if !success { t.Errorf("expected low 4 got %d, high 2 got %d", low, high) } + secondState := ModulesState(modules) + if initialModulesState == secondState { + t.Error("initial state should be different from second") + } low, high = PropagateButtonPress(modules) t.Logf("got low %d and high %d\n", low, high) @@ -64,6 +71,10 @@ func TestPropagateButtonPressExample2FourSteps(t *testing.T) { if !success { t.Errorf("expected low 5 got %d, high 3 got %d", low, high) } + thirdState := ModulesState(modules) + if initialModulesState == thirdState { + t.Error("initial state should be different from third") + } low, high = PropagateButtonPress(modules) t.Logf("got low %d and high %d\n", low, high) @@ -73,4 +84,12 @@ func TestPropagateButtonPressExample2FourSteps(t *testing.T) { t.Errorf("expected low 4 got %d, high 2 got %d", low, high) } + lastState := ModulesState(modules) + + log.Print("initial modules state:\n", initialModulesState) + log.Print("after 4 steps modules state:\n", lastState) + if initialModulesState != lastState { + t.Error("expected state to be same after 4 steps for example 2") + } + } diff --git a/day20/pulsePropagation.go b/day20/pulsePropagation.go index 7c7f85f..74015d2 100644 --- a/day20/pulsePropagation.go +++ b/day20/pulsePropagation.go @@ -12,6 +12,7 @@ func Run() int { filename := "day20/example2" modules := ReadModules(filename) + InitStuffs(modules) log.Print("got modules:\n", modules) low, high := PropagateButtonPress(modules) @@ -97,3 +98,14 @@ func InitStuffs(allModules map[string]Module) { } } + +func ModulesState(allModules 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 { + states[name] = module.StateSnapshot() + } + + return fmt.Sprint(states) +}