From 13888edaff45c4ca515cc22055ea12124b6fb189 Mon Sep 17 00:00:00 2001 From: xuu Date: Wed, 20 Dec 2023 11:27:54 -0700 Subject: [PATCH 1/3] chore: start day20 --- day20/example.txt | 0 day20/input.txt | 0 day20/main.go | 30 ++++++++++++++++++++++++++++++ day20/main_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 day20/example.txt create mode 100644 day20/input.txt create mode 100644 day20/main.go create mode 100644 day20/main_test.go diff --git a/day20/example.txt b/day20/example.txt new file mode 100644 index 0000000..e69de29 diff --git a/day20/input.txt b/day20/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/day20/main.go b/day20/main.go new file mode 100644 index 0000000..c0596d8 --- /dev/null +++ b/day20/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "bufio" + _ "embed" + "fmt" + + aoc "go.sour.is/advent-of-code" +) + +// var log = aoc.Log + +func main() { aoc.MustResult(aoc.Runner(run)) } + +type result struct { + valuePT1 int + valuePT2 int +} + +func (r result) String() string { return fmt.Sprintf("%#v", r) } + +func run(scan *bufio.Scanner) (*result, error) { + + for scan.Scan() { + _ = scan.Text() + + } + + return &result{}, nil +} diff --git a/day20/main_test.go b/day20/main_test.go new file mode 100644 index 0000000..b55d509 --- /dev/null +++ b/day20/main_test.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example.txt +var example []byte + +//go:embed input.txt +var input []byte + +func TestExample(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 0) + is.Equal(result.valuePT2, 0) +} + +func TestSolution(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 0) + is.Equal(result.valuePT2, 0) +} -- 2.45.1 From eb9e770a94bceb8a1656b01b22b7e5010cf04fef Mon Sep 17 00:00:00 2001 From: xuu Date: Thu, 21 Dec 2023 13:18:43 -0700 Subject: [PATCH 2/3] chore: add day20 --- day20/example.txt | 0 day20/example1.txt | 5 + day20/example2.txt | 5 + day20/input.txt | 58 ++++++++ day20/main.go | 359 ++++++++++++++++++++++++++++++++++++++++++++- day20/main_test.go | 27 +++- 6 files changed, 443 insertions(+), 11 deletions(-) delete mode 100644 day20/example.txt create mode 100644 day20/example1.txt create mode 100644 day20/example2.txt diff --git a/day20/example.txt b/day20/example.txt deleted file mode 100644 index e69de29..0000000 diff --git a/day20/example1.txt b/day20/example1.txt new file mode 100644 index 0000000..9ed10dd --- /dev/null +++ b/day20/example1.txt @@ -0,0 +1,5 @@ +broadcaster -> a, b, c +%a -> b +%b -> c +%c -> inv +&inv -> a \ No newline at end of file diff --git a/day20/example2.txt b/day20/example2.txt new file mode 100644 index 0000000..4da4379 --- /dev/null +++ b/day20/example2.txt @@ -0,0 +1,5 @@ +broadcaster -> a +%a -> inv, con +&inv -> b +%b -> con +&con -> output \ No newline at end of file diff --git a/day20/input.txt b/day20/input.txt index e69de29..c2efe97 100644 --- a/day20/input.txt +++ b/day20/input.txt @@ -0,0 +1,58 @@ +%vg -> lf, vd +%dr -> kg +%cn -> mv, pt +%rq -> bk, gr +%vp -> lp, bk +%kg -> lv +%lv -> jc, tp +%sj -> rm, vd +%jc -> tp, qr +%km -> tp, dr +%jx -> cn +&vd -> tf, lf, nb, cx, hx, lr +%lp -> jt, bk +%vj -> ps +broadcaster -> km, lr, xh, rf +%dj -> pt, gc +%cg -> vd, hx +&ln -> tg +%fl -> pt, sk +%lm -> tr, bk +%lr -> vd, vg +&pt -> vq, rf, cm, jx, rg +%cx -> gp +%gp -> vd, sj +&db -> tg +%st -> vd +%jt -> bk +%jh -> lm, bk +%xf -> bd, tp +%gc -> cm, pt +&tp -> dr, km, kg, db, vj, qr +%ps -> xf, tp +%rf -> pt, dj +%lf -> nb +%bd -> tp, gg +%dk -> tp, vj +%mn -> jh, bk +&tg -> rx +%ql -> bk, zx +%tr -> bk, vp +%sk -> pt +%nb -> cg +%sb -> vd, cx +%qr -> dk +%xh -> bk, ql +%rg -> sd +%hx -> sb +%sd -> pt, jx +%gr -> bk, mn +%gg -> tp +%zx -> rq +&bk -> xh, ln, zx +%rm -> st, vd +%hq -> fl, pt +&vq -> tg +%cm -> rg +&tf -> tg +%mv -> pt, hq diff --git a/day20/main.go b/day20/main.go index c0596d8..6b8b413 100644 --- a/day20/main.go +++ b/day20/main.go @@ -4,8 +4,11 @@ import ( "bufio" _ "embed" "fmt" + "os" + "strings" aoc "go.sour.is/advent-of-code" + "golang.org/x/exp/maps" ) // var log = aoc.Log @@ -20,11 +23,357 @@ type result struct { func (r result) String() string { return fmt.Sprintf("%#v", r) } func run(scan *bufio.Scanner) (*result, error) { - - for scan.Scan() { - _ = scan.Text() - + var forever bool + if os.Getenv("AOC_FOREVER") == "1" { + forever = true } - return &result{}, nil + m := &machine{} + receivers := make(map[string][]string) + + m.Add("rx", &rx{}) + + for scan.Scan() { + text := scan.Text() + + name, text, _ := strings.Cut(text, " -> ") + dest := strings.Split(text, ", ") + + switch { + case name == "broadcaster": + m.Add(name, &broadcaster{dest: dest}) + case strings.HasPrefix(name, "%"): + name = strings.TrimPrefix(name, "%") + m.Add(name, &flipflop{name: name, dest: dest}) + + case strings.HasPrefix(name, "&"): + name = strings.TrimPrefix(name, "&") + m.Add(name, &conjuction{name: name, dest: dest}) + } + + for _, d := range dest { + receivers[d] = append(receivers[d], name) + } + } + + m.Setup(receivers) + + result := &result{} + + if forever { + i := 0 + + defer func() { + if p := recover(); p != nil { + fmt.Printf("## Press %d FINISH %v ##", i, p) + os.Exit(1) + } + }() + + for { + if i%12345 == 0 { + fmt.Printf("## Press %d ##\r", i) + } + m.Push(i) + i++ + } + } + + for i := 0; i < 4_0000; i++ { + // fmt.Printf("\n## Press %d ##\n\n", i) + if i == 1000 { + result.valuePT1 = m.highPulses * m.lowPulses + } + m.Push(i) + + } + + // fmt.Println("\n## SUMMARY ##") + // fmt.Println("Sent", LOW, m.lowPulses) + // fmt.Println("Sent", HIGH, m.highPulses) + var lvalues []int + + for _, p := range m.m { + if p, ok := p.(*conjuction); ok && in(p.name, []string{"bk","tp","pt","vd"}) { + var values []int + for k, v := range p.pushes { + for i, h := range makeHistory(v) { + if i == 1 && len(h) > 3 && h[0] > 0 { //&& all(h[0], h[1:]...) { + fmt.Println(p.name, k, i, h[0]) + values = append(values, h[0]) + } + } + } + max := aoc.Max(values[0], values...) + fmt.Println(p.name, "MAX", max, values) + lvalues = append(lvalues, max) + } + } + result.valuePT2 = aoc.LCM(lvalues...) + fmt.Println("tg", "LCM", result.valuePT2, lvalues) + + // trace("rx", receivers) + + return result, nil +} + +type signal bool + +const ( + LOW signal = false + HIGH signal = true +) + +func (m signal) String() string { + if m { + return " >>-HIGH-> " + } + return " >>-LOW-> " +} + +type message struct { + signal + from, to string +} + +func (m message) String() string { + return fmt.Sprint(m.from, m.signal, m.to) +} + +type pulser interface { + Pulse(message) + SetMachine(*machine) +} + +type machine struct { + m map[string]pulser + press int + + queue []message + hwm int + + stop bool + + highPulses int + lowPulses int +} + +func (m *machine) Add(name string, p pulser) { + if m.m == nil { + m.m = make(map[string]pulser) + } + p.SetMachine(m) + m.m[name] = p +} +func (m *machine) Send(msgs ...message) { + m.queue = append(m.queue, msgs...) + for _, msg := range msgs { + // fmt.Println(msg) + if msg.signal { + m.highPulses++ + } else { + m.lowPulses++ + } + } +} + +func (m *machine) Push(i int) { + m.press = i + m.Send(generate(LOW, "button", "broadcaster")...) + m.processQueue(i) +} + +func (m *machine) processQueue(i int) { + // look for work and process up to the queue length. repeat. + hwm := 0 + for hwm < len(m.queue) { + end := len(m.queue) + + for ; hwm < end; hwm++ { + msg := m.queue[hwm] + + if p, ok := m.m[msg.to]; ok { + // fmt.Println(i, "S:", m.m[msg.from], msg.signal, "R:", p) + p.Pulse(msg) + } + } + + hwm = 0 + copy(m.queue, m.queue[end:]) + m.queue = m.queue[:len(m.queue)-end] + // fmt.Println("") + } +} +func (m *machine) Setup(receivers map[string][]string) { + for name, recv := range receivers { + if p, ok := m.m[name]; ok { + if p, ok := p.(interface{ Receive(...string) }); ok { + p.Receive(recv...) + } + } + } +} + +func (m *machine) Stop() { + m.stop = true +} + +type IsModule struct { + *machine +} + +func (p *IsModule) SetMachine(m *machine) { p.machine = m } + +type broadcaster struct { + dest []string + IsModule +} + +func (b *broadcaster) Pulse(msg message) { + b.Send(generate(msg.signal, "broadcaster", b.dest...)...) +} +func (b *broadcaster) String() string { return "br" } + +type flipflop struct { + name string + state signal + dest []string + + IsModule +} + +func (b *flipflop) Pulse(msg message) { + if !msg.signal { + b.state = !b.state + b.Send(generate(b.state, b.name, b.dest...)...) + } +} +func (b *flipflop) String() string { + return fmt.Sprintf("%s(%v)", b.name, b.state) +} + +type conjuction struct { + name string + state map[string]signal + dest []string + + pushes map[string][]int + activate []int + last map[string]int + max map[string]int + lcm int + + IsModule +} + +func (b *conjuction) Receive(names ...string) { + if b.state == nil { + b.state = make(map[string]signal) + b.last = make(map[string]int) + b.max = make(map[string]int) + b.pushes = make(map[string][]int) + } + for _, name := range names { + b.state[name] = false + b.max[name] = int(^uint(0)>>1) + b.pushes[name] = []int{} + + } +} +func (b *conjuction) Pulse(msg message) { + if b.state == nil { + b.state = make(map[string]signal) + b.last = make(map[string]int) + b.max = make(map[string]int) + } + + b.state[msg.from] = msg.signal + + if msg.signal { + b.pushes[msg.from] = append(b.pushes[msg.from], b.press) + b.last[msg.from] = b.press - b.last[msg.from] + + + // vals := maps.Values(b.max) + // if aoc.Min(vals[0], vals...) != 0 { + // if lcm := aoc.LCM(vals...); lcm > 0 { + // fmt.Printf("\nfound loop %s = %d %v\n", b.name, lcm, vals) + // } + // } + } + + if all(HIGH, maps.Values(b.state)...) { + b.activate = append(b.activate, b.press) + b.Send(generate(LOW, b.name, b.dest...)...) + return + } + b.Send(generate(HIGH, b.name, b.dest...)...) +} +func (b *conjuction) String() string { + return fmt.Sprintf("%s(%v)", b.name, b.state) +} + +type rx struct { + IsModule +} + +func (rx *rx) Pulse(msg message) { + if !msg.signal { + panic("pulse received") + } +} +func (rx *rx) String() string { return "rx" } + +func all[T ~int|~bool](match T, lis ...T) bool { + if len(lis) == 0 { + return true + } + + for _, b := range lis { + if b != match { + return false + } + } + + return true +} +func generate(t signal, from string, destinations ...string) []message { + msgs := make([]message, len(destinations)) + for i, to := range destinations { + msgs[i] = message{signal: t, from: from, to: to} + } + return msgs +} + +func in(n string, haystack []string) bool { + for _, h := range haystack { + if n == h { + return true + } + } + return false +} + +func makeHistory(in []int) [][]int { + var history [][]int + history = append(history, in) + + for { + var diffs []int + + + current := history[len(history)-1] + if len(current) == 0 { return nil } + + for i := range current[1:] { + diffs = append(diffs, current[i+1]-current[i]) + } + + history = append(history, diffs) + + if len(diffs) == 0 || aoc.Max(diffs[0], diffs[1:]...) == 0 && aoc.Min(diffs[0], diffs[1:]...) == 0 { + break + } + } + return history } diff --git a/day20/main_test.go b/day20/main_test.go index b55d509..8af5bb6 100644 --- a/day20/main_test.go +++ b/day20/main_test.go @@ -10,21 +10,36 @@ import ( "github.com/matryer/is" ) -//go:embed example.txt -var example []byte +//go:embed example1.txt +var example1 []byte + +//go:embed example2.txt +var example2 []byte //go:embed input.txt var input []byte -func TestExample(t *testing.T) { +func TestExample1(t *testing.T) { is := is.New(t) - scan := bufio.NewScanner(bytes.NewReader(example)) + scan := bufio.NewScanner(bytes.NewReader(example1)) result, err := run(scan) is.NoErr(err) t.Log(result) - is.Equal(result.valuePT1, 0) + is.Equal(result.valuePT1, 32000000) + is.Equal(result.valuePT2, 0) +} + +func TestExample2(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example2)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 11687500) is.Equal(result.valuePT2, 0) } @@ -36,6 +51,6 @@ func TestSolution(t *testing.T) { is.NoErr(err) t.Log(result) - is.Equal(result.valuePT1, 0) + is.Equal(result.valuePT1, 819397964) is.Equal(result.valuePT2, 0) } -- 2.45.1 From 761013df81421ee1cf46061d0b427d3d29ca76e6 Mon Sep 17 00:00:00 2001 From: xuu Date: Thu, 21 Dec 2023 16:36:19 -0700 Subject: [PATCH 3/3] chore: cleanup day 20 --- day20/main.go | 187 +++++++++++---------------------------------- day20/main_test.go | 2 +- 2 files changed, 46 insertions(+), 143 deletions(-) diff --git a/day20/main.go b/day20/main.go index 6b8b413..fdcd47c 100644 --- a/day20/main.go +++ b/day20/main.go @@ -4,7 +4,6 @@ import ( "bufio" _ "embed" "fmt" - "os" "strings" aoc "go.sour.is/advent-of-code" @@ -23,16 +22,9 @@ type result struct { func (r result) String() string { return fmt.Sprintf("%#v", r) } func run(scan *bufio.Scanner) (*result, error) { - var forever bool - if os.Getenv("AOC_FOREVER") == "1" { - forever = true - } - m := &machine{} receivers := make(map[string][]string) - m.Add("rx", &rx{}) - for scan.Scan() { text := scan.Text() @@ -48,72 +40,46 @@ func run(scan *bufio.Scanner) (*result, error) { case strings.HasPrefix(name, "&"): name = strings.TrimPrefix(name, "&") - m.Add(name, &conjuction{name: name, dest: dest}) + m.Add(name, &conjunction{name: name, dest: dest}) } for _, d := range dest { + // rx is present so enable pt 2 + if d == "rx" { + m.Add("rx", &rx{}) + } receivers[d] = append(receivers[d], name) } } - m.Setup(receivers) + m.setup(receivers) result := &result{} - if forever { - i := 0 - - defer func() { - if p := recover(); p != nil { - fmt.Printf("## Press %d FINISH %v ##", i, p) - os.Exit(1) - } - }() - - for { - if i%12345 == 0 { - fmt.Printf("## Press %d ##\r", i) - } - m.Push(i) - i++ - } - } - - for i := 0; i < 4_0000; i++ { - // fmt.Printf("\n## Press %d ##\n\n", i) + for i := 0; i < 10_000; i++ { // need enough presses to find the best LCM values for each conjunction if i == 1000 { result.valuePT1 = m.highPulses * m.lowPulses } m.Push(i) - } - // fmt.Println("\n## SUMMARY ##") - // fmt.Println("Sent", LOW, m.lowPulses) - // fmt.Println("Sent", HIGH, m.highPulses) - var lvalues []int + // rx is present.. perform part 2. + if rx, ok := receivers["rx"]; ok { + tip := m.m[rx[0]].(*conjunction) // panic if missing! - for _, p := range m.m { - if p, ok := p.(*conjuction); ok && in(p.name, []string{"bk","tp","pt","vd"}) { - var values []int - for k, v := range p.pushes { - for i, h := range makeHistory(v) { - if i == 1 && len(h) > 3 && h[0] > 0 { //&& all(h[0], h[1:]...) { - fmt.Println(p.name, k, i, h[0]) - values = append(values, h[0]) - } + var lvalues []int + for k, v := range tip.pushes { + for i, h := range makeHistory(v) { + if i == 1 && len(h) > 0 && h[0] > 0 { + fmt.Println(tip.name, k, "frequency", h[0]) + lvalues = append(lvalues, h[0]) } } - max := aoc.Max(values[0], values...) - fmt.Println(p.name, "MAX", max, values) - lvalues = append(lvalues, max) } + + result.valuePT2 = aoc.LCM(lvalues...) + fmt.Println(tip.name, "LCM", result.valuePT2, lvalues) } - result.valuePT2 = aoc.LCM(lvalues...) - fmt.Println("tg", "LCM", result.valuePT2, lvalues) - - // trace("rx", receivers) - return result, nil } @@ -124,36 +90,17 @@ const ( HIGH signal = true ) -func (m signal) String() string { - if m { - return " >>-HIGH-> " - } - return " >>-LOW-> " -} - type message struct { signal from, to string } -func (m message) String() string { - return fmt.Sprint(m.from, m.signal, m.to) -} - -type pulser interface { - Pulse(message) - SetMachine(*machine) -} - type machine struct { m map[string]pulser - press int queue []message - hwm int - - stop bool + press int highPulses int lowPulses int } @@ -176,13 +123,11 @@ func (m *machine) Send(msgs ...message) { } } } - func (m *machine) Push(i int) { m.press = i m.Send(generate(LOW, "button", "broadcaster")...) m.processQueue(i) } - func (m *machine) processQueue(i int) { // look for work and process up to the queue length. repeat. hwm := 0 @@ -204,7 +149,7 @@ func (m *machine) processQueue(i int) { // fmt.Println("") } } -func (m *machine) Setup(receivers map[string][]string) { +func (m *machine) setup(receivers map[string][]string) { for name, recv := range receivers { if p, ok := m.m[name]; ok { if p, ok := p.(interface{ Receive(...string) }); ok { @@ -214,10 +159,12 @@ func (m *machine) Setup(receivers map[string][]string) { } } -func (m *machine) Stop() { - m.stop = true +type pulser interface { + Pulse(message) + SetMachine(*machine) } +// IsModule implements the machine registration for each module. type IsModule struct { *machine } @@ -232,7 +179,6 @@ type broadcaster struct { func (b *broadcaster) Pulse(msg message) { b.Send(generate(msg.signal, "broadcaster", b.dest...)...) } -func (b *broadcaster) String() string { return "br" } type flipflop struct { name string @@ -248,70 +194,41 @@ func (b *flipflop) Pulse(msg message) { b.Send(generate(b.state, b.name, b.dest...)...) } } -func (b *flipflop) String() string { - return fmt.Sprintf("%s(%v)", b.name, b.state) -} -type conjuction struct { +type conjunction struct { name string state map[string]signal dest []string pushes map[string][]int - activate []int - last map[string]int - max map[string]int - lcm int IsModule } -func (b *conjuction) Receive(names ...string) { +func (b *conjunction) Receive(names ...string) { if b.state == nil { b.state = make(map[string]signal) - b.last = make(map[string]int) - b.max = make(map[string]int) b.pushes = make(map[string][]int) } for _, name := range names { b.state[name] = false - b.max[name] = int(^uint(0)>>1) b.pushes[name] = []int{} - } } -func (b *conjuction) Pulse(msg message) { - if b.state == nil { - b.state = make(map[string]signal) - b.last = make(map[string]int) - b.max = make(map[string]int) - } - +func (b *conjunction) Pulse(msg message) { b.state[msg.from] = msg.signal if msg.signal { + // collect frequency of pushes to esti,ate rate b.pushes[msg.from] = append(b.pushes[msg.from], b.press) - b.last[msg.from] = b.press - b.last[msg.from] - - - // vals := maps.Values(b.max) - // if aoc.Min(vals[0], vals...) != 0 { - // if lcm := aoc.LCM(vals...); lcm > 0 { - // fmt.Printf("\nfound loop %s = %d %v\n", b.name, lcm, vals) - // } - // } } if all(HIGH, maps.Values(b.state)...) { - b.activate = append(b.activate, b.press) b.Send(generate(LOW, b.name, b.dest...)...) return } b.Send(generate(HIGH, b.name, b.dest...)...) } -func (b *conjuction) String() string { - return fmt.Sprintf("%s(%v)", b.name, b.state) -} type rx struct { IsModule @@ -319,16 +236,12 @@ type rx struct { func (rx *rx) Pulse(msg message) { if !msg.signal { - panic("pulse received") + panic("pulse received") // will never happen... } } -func (rx *rx) String() string { return "rx" } - -func all[T ~int|~bool](match T, lis ...T) bool { - if len(lis) == 0 { - return true - } +// helper funcs +func all[T comparable](match T, lis ...T) bool { for _, b := range lis { if b != match { return false @@ -345,35 +258,25 @@ func generate(t signal, from string, destinations ...string) []message { return msgs } -func in(n string, haystack []string) bool { - for _, h := range haystack { - if n == h { - return true - } - } - return false -} - +// makeHistory from day 9 func makeHistory(in []int) [][]int { var history [][]int history = append(history, in) - for { - var diffs []int - + // for { + var diffs []int - current := history[len(history)-1] - if len(current) == 0 { return nil } + current := history[len(history)-1] - for i := range current[1:] { - diffs = append(diffs, current[i+1]-current[i]) - } - - history = append(history, diffs) - - if len(diffs) == 0 || aoc.Max(diffs[0], diffs[1:]...) == 0 && aoc.Min(diffs[0], diffs[1:]...) == 0 { - break - } + for i := range current[1:] { + diffs = append(diffs, current[i+1]-current[i]) } + + history = append(history, diffs) + + // if len(diffs) == 0 || aoc.Max(diffs[0], diffs[1:]...) == 0 && aoc.Min(diffs[0], diffs[1:]...) == 0 { + // break + // } + // } return history } diff --git a/day20/main_test.go b/day20/main_test.go index 8af5bb6..1ab6824 100644 --- a/day20/main_test.go +++ b/day20/main_test.go @@ -52,5 +52,5 @@ func TestSolution(t *testing.T) { t.Log(result) is.Equal(result.valuePT1, 819397964) - is.Equal(result.valuePT2, 0) + is.Equal(result.valuePT2, 252667369442479) } -- 2.45.1