further noodling on day 18

This commit is contained in:
David 2021-12-23 09:52:58 -07:00
parent 917be0d8f1
commit cc7256461e
2 changed files with 127 additions and 6 deletions

View File

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

View File

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