day20, example 2 first four steps pass

This commit is contained in:
efim 2023-12-20 09:45:52 +00:00
parent 1d7a0ef7b8
commit 00e60657fa
3 changed files with 131 additions and 0 deletions

View File

@ -9,6 +9,10 @@ import (
)
type PulseType int
func (pt PulseType)String() string {
types := []string{"high", "low"}
return types[pt]
}
const (
HighPulse PulseType = iota

76
day20/propagation_test.go Normal file
View File

@ -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)
}
}

View File

@ -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)
}
}
}