Compare commits

..

11 Commits

Author SHA1 Message Date
xuu
7847d11f95
chore: add day 17 pt 2
Some checks failed
Go Test / build (pull_request) Failing after 10m29s
2023-12-26 13:29:48 -07:00
xuu
f0696a6fe6 Merge pull request 'add day 20' (#13) from day20 into main
All checks were successful
Go Bump / bump (push) Successful in 11s
Go Test / build (push) Successful in 37s
Reviewed-on: #13
2023-12-26 13:03:00 -07:00
xuu
786a5bae35
Merge branch 'main' into day20
All checks were successful
Go Test / build (pull_request) Successful in 38s
2023-12-26 13:02:01 -07:00
xuu
e82c9a97c4 Merge pull request 'chore: add day 17 pt 2' (#14) from day17 into main
Some checks failed
Go Bump / bump (push) Successful in 10s
Go Test / build (push) Failing after 10m29s
Reviewed-on: #14
2023-12-26 12:44:04 -07:00
xuu
b2da6a35e7 Merge branch 'main' into day17
Some checks failed
Go Test / build (pull_request) Failing after 10m28s
2023-12-26 12:43:56 -07:00
xuu
970f5b2d76
chore: add day 17 pt 2
Some checks failed
Go Test / build (pull_request) Failing after 10m27s
2023-12-26 12:42:11 -07:00
xuu
8e451050b1 Merge branch 'main' into day20
Some checks failed
Go Test / build (pull_request) Failing after 43s
2023-12-21 17:04:25 -07:00
xuu
761013df81
chore: cleanup day 20
All checks were successful
Go Test / build (pull_request) Successful in 34s
2023-12-21 16:44:35 -07:00
xuu
6a03be9ca9 Merge pull request 'chore: add day17' (#11) from day17 into main
Some checks failed
Go Bump / bump (push) Successful in 8s
Go Test / build (push) Failing after 36s
Reviewed-on: #11
2023-12-21 16:39:16 -07:00
xuu
eb9e770a94
chore: add day20 2023-12-21 13:18:59 -07:00
xuu
13888edaff
chore: start day20 2023-12-20 11:27:54 -07:00
5 changed files with 406 additions and 0 deletions

5
day20/example1.txt Normal file
View File

@ -0,0 +1,5 @@
broadcaster -> a, b, c
%a -> b
%b -> c
%c -> inv
&inv -> a

5
day20/example2.txt Normal file
View File

@ -0,0 +1,5 @@
broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output

58
day20/input.txt Normal file
View File

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

282
day20/main.go Normal file
View File

@ -0,0 +1,282 @@
package main
import (
"bufio"
_ "embed"
"fmt"
"strings"
aoc "go.sour.is/advent-of-code"
"golang.org/x/exp/maps"
)
// 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) {
m := &machine{}
receivers := make(map[string][]string)
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, &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)
result := &result{}
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)
}
// rx is present.. perform part 2.
if rx, ok := receivers["rx"]; ok {
tip := m.m[rx[0]].(*conjunction) // panic if missing!
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])
}
}
}
result.valuePT2 = aoc.LCM(lvalues...)
fmt.Println(tip.name, "LCM", result.valuePT2, lvalues)
}
return result, nil
}
type signal bool
const (
LOW signal = false
HIGH signal = true
)
type message struct {
signal
from, to string
}
type machine struct {
m map[string]pulser
queue []message
press int
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...)
}
}
}
}
type pulser interface {
Pulse(message)
SetMachine(*machine)
}
// IsModule implements the machine registration for each module.
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...)...)
}
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...)...)
}
}
type conjunction struct {
name string
state map[string]signal
dest []string
pushes map[string][]int
IsModule
}
func (b *conjunction) Receive(names ...string) {
if b.state == nil {
b.state = make(map[string]signal)
b.pushes = make(map[string][]int)
}
for _, name := range names {
b.state[name] = false
b.pushes[name] = []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)
}
if all(HIGH, maps.Values(b.state)...) {
b.Send(generate(LOW, b.name, b.dest...)...)
return
}
b.Send(generate(HIGH, b.name, b.dest...)...)
}
type rx struct {
IsModule
}
func (rx *rx) Pulse(msg message) {
if !msg.signal {
panic("pulse received") // will never happen...
}
}
// helper funcs
func all[T comparable](match T, lis ...T) bool {
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
}
// makeHistory from day 9
func makeHistory(in []int) [][]int {
var history [][]int
history = append(history, in)
// for {
var diffs []int
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
// }
// }
return history
}

56
day20/main_test.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"bufio"
"bytes"
"testing"
_ "embed"
"github.com/matryer/is"
)
//go:embed example1.txt
var example1 []byte
//go:embed example2.txt
var example2 []byte
//go:embed input.txt
var input []byte
func TestExample1(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(example1))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
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)
}
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, 819397964)
is.Equal(result.valuePT2, 252667369442479)
}