From cc7256461efd45bd371395de1a46d62a4e52e94a Mon Sep 17 00:00:00 2001 From: David Ashby Date: Thu, 23 Dec 2021 09:52:58 -0700 Subject: [PATCH] further noodling on day 18 --- 18/main.go | 55 ++++++++++++++++++++++++++++++---- 18/main_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 6 deletions(-) diff --git a/18/main.go b/18/main.go index 0a56f73..39e193c 100644 --- a/18/main.go +++ b/18/main.go @@ -117,18 +117,32 @@ func Add(left, right *pair) *pair { } } -func Reduce(p *pair) *pair { - return &pair{} // ??? +func Reduce(p *pair) { + for { + if MustExplode(0, p) { + Explode(p) + } else if MustSplit(p) { + Split(p) + } else { + break + } + } } -func Split(p *pair) *pair { +func Split(p *pair) bool { if p.left > 9 { p.leftp = &pair{ left: p.left / 2, right: (p.left / 2) + (p.left % 2), } p.left = 0 - return p + return true + } + if p.leftp != nil { + m := Split(p.leftp) + if m { + return true + } } if p.right > 9 { p.rightp = &pair{ @@ -136,9 +150,34 @@ func Split(p *pair) *pair { right: (p.right / 2) + (p.right % 2), } p.right = 0 - return p + return true } - return p + if p.rightp != nil { + m := Split(p.rightp) + if m { + return true + } + } + return false +} + +func MustSplit(p *pair) bool { + if p.left > 9 || p.right > 9 { + return true + } + if p.leftp != nil { + m := MustSplit(p.leftp) + if m { + return true + } + } + if p.rightp != nil { + m := MustSplit(p.rightp) + if m { + return true + } + } + return false } func MustExplode(depth int, p *pair) bool { @@ -160,6 +199,10 @@ func MustExplode(depth int, p *pair) bool { return false } +func Explode(p *pair) { + +} + func partOne() { scanner := makeScanner(false) diff --git a/18/main_test.go b/18/main_test.go index 61eb6f5..8b4d899 100644 --- a/18/main_test.go +++ b/18/main_test.go @@ -43,6 +43,58 @@ func TestMustExplode(t *testing.T) { } } +func TestSplit(t *testing.T) { + cases := map[*pair]string{ + {left: 1, right: 1}: "[1,1]", + {left: 10, right: 1}: "[[5,5],1]", + {left: 11, right: 1}: "[[5,6],1]", + {left: 12, right: 1}: "[[6,6],1]", + {left: 12, right: 12}: "[[6,6],12]", + {left: 2, right: 12}: "[2,[6,6]]", + { + leftp: &pair{left: 15, right: 9}, + rightp: &pair{left: 8, right: 1}, + }: "[[[7,8],9],[8,1]]", + { + leftp: &pair{left: 1, right: 9}, + rightp: &pair{left: 8, right: 15}, + }: "[[1,9],[8,[7,8]]]", + } + for input, expected := range cases { + Split(input) + if input.String() != expected { + t.Log(input.String(), expected) + t.Fail() + } + } +} + +func TestMustSplit(t *testing.T) { + cases := map[*pair]bool{ + {left: 1, right: 1}: false, + {left: 10, right: 1}: true, + {left: 11, right: 1}: true, + {left: 12, right: 1}: true, + {left: 12, right: 12}: true, + {left: 2, right: 12}: true, + { + leftp: &pair{left: 1, right: 9}, + rightp: &pair{left: 8, right: 5}, + }: false, + { + leftp: &pair{left: 1, right: 9}, + rightp: &pair{left: 8, right: 15}, + }: true, + } + for input, output := range cases { + result := MustSplit(input) + if result != output { + t.Log(input.String()) + t.Fail() + } + } +} + func TestAdd(t *testing.T) { cases := map[string][]*pair{ "[[1,2],[[3,4],5]]": { @@ -59,6 +111,32 @@ func TestAdd(t *testing.T) { } } +func TestReduce(t *testing.T) { + cases := map[*pair]string{ + {left: 1, right: 1}: "[1,1]", + {left: 10, right: 1}: "[[5,5],1]", + {left: 11, right: 1}: "[[5,6],1]", + {left: 12, right: 1}: "[[6,6],1]", + {left: 12, right: 13}: "[[6,6],[6,7]]", + {left: 2, right: 12}: "[2,[6,6]]", + { + leftp: &pair{left: 15, right: 9}, + rightp: &pair{left: 8, right: 1}, + }: "[[[7,8],9],[8,1]]", + { + leftp: &pair{left: 1, right: 9}, + rightp: &pair{left: 8, right: 15}, + }: "[[1,9],[8,[7,8]]]", + } + for input, expected := range cases { + Reduce(input) + if input.String() != expected { + t.Log(input.String(), expected) + t.Fail() + } + } +} + func TestMagnitude(t *testing.T) { cases := map[int]*pair{ 29: {left: 9, right: 1},