Working on 2021 day 18

This commit is contained in:
Brian Buller 2021-12-18 09:47:42 -06:00
parent 72be8f1ac5
commit 8a4a437947
5 changed files with 315 additions and 1 deletions

100
2021/day18/input Normal file
View File

@ -0,0 +1,100 @@
[[[0,[6,6]],[[7,2],[6,2]]],[[[9,4],[5,8]],6]]
[[[4,9],6],[[[0,1],[8,5]],[3,[7,6]]]]
[[1,7],[[[1,3],2],[[6,8],8]]]
[4,[[6,[6,0]],[[4,9],5]]]
[[3,8],[[0,6],1]]
[[2,2],[[[2,3],[2,9]],[[9,6],[4,9]]]]
[[[3,4],[[7,7],[7,8]]],[[0,2],6]]
[[[[4,4],[3,3]],[[9,0],4]],[8,[7,[6,6]]]]
[[[9,9],8],1]
[2,[[7,[1,9]],[9,2]]]
[[[[6,0],[8,2]],[[9,0],[8,7]]],[3,[6,[8,8]]]]
[[[8,[9,2]],[1,4]],[[2,2],[[1,0],5]]]
[[[9,[0,3]],[4,[2,3]]],[[8,[0,1]],[[4,8],[5,4]]]]
[[[2,0],[1,7]],[[3,[0,7]],[[7,6],0]]]
[[[1,[5,1]],[[0,3],9]],[[3,8],[[5,3],1]]]
[[[[4,5],2],[[7,7],[4,8]]],[3,[3,[6,5]]]]
[[[[4,4],4],[[5,0],2]],[9,[[0,7],5]]]
[[2,[[8,0],9]],0]
[[[[0,5],[1,0]],[0,[5,1]]],[[[0,8],[6,0]],[6,[3,9]]]]
[[[[2,4],[5,5]],[4,7]],[[[5,6],[9,5]],1]]
[[[4,[5,1]],[[6,7],1]],[1,[[5,9],4]]]
[[0,6],[[9,3],[4,2]]]
[7,[[[2,4],[3,4]],[2,0]]]
[0,[6,7]]
[[[0,2],[[7,2],7]],[[[6,3],[2,0]],[[8,6],[7,9]]]]
[[[7,[0,5]],7],[[2,3],[3,3]]]
[[2,[0,[4,5]]],[[4,[7,9]],[[5,6],4]]]
[[[5,4],0],[5,[[7,1],[7,6]]]]
[[0,3],[[[5,2],5],[[8,5],[2,8]]]]
[[[0,[8,9]],[[3,1],2]],[[3,[7,1]],7]]
[[[[9,7],[5,5]],[[2,4],1]],[[1,3],[[4,0],[9,1]]]]
[[[0,9],4],0]
[[[[8,8],9],[[8,2],3]],[[2,[7,4]],[9,1]]]
[[[1,2],8],[[[4,4],2],[2,4]]]
[[6,[7,[2,1]]],[[2,1],3]]
[0,[[[7,8],0],[5,[8,5]]]]
[[[2,8],6],[6,[[1,1],[1,2]]]]
[[[[3,9],2],[7,[4,9]]],[[[5,3],3],[[7,3],5]]]
[[2,[4,[3,2]]],[[4,9],6]]
[[[[1,1],[0,5]],[1,[4,9]]],[[6,[5,7]],[[1,6],[7,2]]]]
[0,[0,[[8,5],8]]]
[[[1,[2,5]],8],[[3,[0,2]],4]]
[[[4,3],0],[[[6,9],[7,2]],[[1,9],8]]]
[[[[7,1],0],[7,5]],[6,2]]
[[[9,[6,5]],6],[[5,5],[[4,6],2]]]
[[[[3,0],[5,5]],2],[7,[9,[8,5]]]]
[9,[[9,7],[3,3]]]
[[[0,0],4],[7,[[5,8],[2,7]]]]
[[[[9,2],4],[[0,1],[4,1]]],[[4,[6,5]],5]]
[8,[[[5,2],8],[6,0]]]
[[[[9,6],2],9],[5,[[5,3],5]]]
[[[8,[8,0]],[7,[3,3]]],[[[8,8],[5,5]],[[1,3],3]]]
[[[2,3],4],[[[8,8],[1,4]],5]]
[[[[3,7],9],6],[[5,[4,1]],4]]
[[[3,4],[[5,3],4]],[[2,[4,2]],[[0,7],5]]]
[0,9]
[[2,[[9,1],[3,4]]],[[[5,7],[6,6]],[5,[3,6]]]]
[[[[7,2],7],[[8,7],9]],[9,[[7,0],[3,4]]]]
[[[[8,3],[0,2]],0],[5,[[9,9],1]]]
[[[1,5],[[3,9],[5,6]]],[6,[2,[2,4]]]]
[[[[1,6],7],[8,9]],[[[6,7],8],1]]
[[5,[[1,3],[1,8]]],[8,1]]
[[[[4,6],9],[[3,0],[2,4]]],3]
[[[[6,6],[9,8]],[4,7]],[[6,8],1]]
[[[9,[7,8]],[4,[0,3]]],[[8,[8,1]],8]]
[4,0]
[[[[1,8],[6,9]],[[1,5],[6,1]]],[[1,9],[0,1]]]
[[[[1,8],[1,8]],[[6,2],[8,6]]],[[[6,8],3],[[8,0],[7,3]]]]
[8,9]
[[2,8],2]
[[4,[[7,0],[1,8]]],1]
[[5,0],[[[3,4],0],3]]
[[[[7,5],2],6],[[9,2],[[5,0],[7,5]]]]
[[8,[[0,0],[3,7]]],[1,6]]
[[[[5,2],[5,1]],[8,6]],[2,[9,[5,4]]]]
[[[6,[3,3]],[[0,4],0]],[[4,4],[[3,6],6]]]
[[7,[[4,9],[9,7]]],[3,[[8,7],9]]]
[[[[3,3],9],[8,[7,2]]],[[3,9],8]]
[[[0,[9,2]],1],[6,[[9,7],8]]]
[[5,[0,1]],[8,7]]
[[[[0,7],5],8],[[[0,0],[7,1]],[2,[6,9]]]]
[[5,[[3,3],7]],[[[1,3],[5,4]],[[0,8],[2,2]]]]
[[[[7,4],5],[[9,1],[5,5]]],[[0,8],[0,[6,4]]]]
[[0,[[9,5],[4,3]]],[[[2,7],[4,7]],6]]
[[7,[[3,2],[7,9]]],[[[8,6],[1,8]],[5,[5,6]]]]
[[0,[[3,3],0]],[[[5,2],[2,4]],[6,8]]]
[[[6,[5,6]],9],[[[5,2],7],[[8,7],[2,4]]]]
[[[[7,2],[8,2]],5],[[[4,5],[0,7]],6]]
[[[[6,1],4],6],[[4,[7,8]],3]]
[[[[2,6],[5,0]],5],[[3,[5,4]],[[2,3],[8,6]]]]
[[4,[[1,3],[1,3]]],[[[5,1],2],[[7,3],[9,6]]]]
[[8,[6,[0,0]]],[[[4,4],9],[6,2]]]
[[9,7],7]
[[[3,1],[[0,3],[5,2]]],3]
[[1,5],[4,[0,[3,6]]]]
[[[[4,9],[6,5]],[8,4]],2]
[[[7,[1,5]],4],[[3,[4,6]],[7,[4,3]]]]
[[[0,[5,7]],[[8,7],[2,6]]],[[4,5],5]]
[[[2,3],[[9,5],[9,3]]],[[[2,5],9],[[9,1],8]]]
[[[[4,4],4],[[4,0],9]],[[[5,3],1],[3,[7,6]]]]

149
2021/day18/main.go Normal file
View File

@ -0,0 +1,149 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
var build *SNum
for i := range inp {
wrk, _ := ParseSNum(inp[i])
if build == nil {
build = wrk
} else {
build = build.add(wrk)
}
}
fmt.Println(build)
v := build.needsExplode(0)
fmt.Println("Needs Explode:", v)
v.explode()
fmt.Println(build)
/*
full := inp[0]
for i := 1; i < len(inp); i++ {
full = fmt.Sprintf("[%s,%s]", full, inp[i])
}
*/
}
type SNum struct {
parent, left, right *SNum
regular bool
value int
}
// ParseSNum returns an SNum and what's left of inp
// after it was produced
func ParseSNum(inp string) (*SNum, string) {
// inp should always start with either a number or a '['
var rest string
s := SNum{}
if inp[0] == '[' {
s.regular = false
s.left, rest = ParseSNum(inp[1:])
s.right, rest = ParseSNum(rest[1:])
s.left.parent = &s
s.right.parent = &s
return &s, rest[1:]
} else {
s.regular = true
var num []byte
for i := range inp {
if inp[i] >= '0' && inp[i] <= '9' {
num = append(num, inp[i])
} else {
s.value = h.Atoi(string(num))
return &s, inp[i:]
}
}
}
return &s, rest
}
func (s *SNum) needsExplode(depth int) *SNum {
if depth == 4 && !s.regular {
return s
}
if s.regular {
return nil
}
if tst := s.left.needsExplode(depth + 1); tst != nil {
return tst
} else if tst := s.right.needsExplode(depth + 1); tst != nil {
return tst
}
return nil
}
func (s *SNum) explode() {
if s.parent == nil {
return
}
s.parent.addToRegularRightOf(s.right.value, s)
s.parent.addToRegularLeftOf(s.left.value, s)
}
func (s *SNum) addToRegularLeftOf(v int, c *SNum) {
if s.right.regular {
s.right.value += v
} else {
if s.right != nil && s.right == c {
r := s.left.findRightmostRegular()
if r != nil {
r.value += v
}
} else if s.left != nil && s.left == c {
s.parent.addToRegularLeftOf(v, s)
}
}
}
func (s *SNum) addToRegularRightOf(v int, c *SNum) {
if s.left.regular {
s.left.value += v
} else if s.left != nil && s.left == c {
l := s.right.findLeftmostRegular()
if l != nil {
l.value += v
}
} else if s.right != nil && s.right == c {
s.parent.addToRegularRightOf(v, s)
}
}
func (s *SNum) contains(n *SNum) bool {
return s == n || s.left.contains(n) || s.right.contains(n)
}
func (s *SNum) findRightmostRegular() *SNum {
if s.regular {
return s
}
return s.right.findRightmostRegular()
}
func (s *SNum) findLeftmostRegular() *SNum {
if s.regular {
return s
}
return s.left.findLeftmostRegular()
}
func (s *SNum) add(n *SNum) *SNum {
return &SNum{
left: s,
right: n,
}
}
func (s SNum) String() string {
if s.regular {
return fmt.Sprintf("%d", s.value)
}
return fmt.Sprintf("[%s,%s]", s.left, s.right)
}

View File

@ -0,0 +1,58 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
build := inp[0]
for _, v := range inp[1:] {
build = fmt.Sprintf("[%s,%s]", build, v)
}
// Keep on steppin' until no change happens
var wrk string
var done bool
for !done {
wrk = step(build)
done = wrk == build
build = wrk
}
fmt.Println(wrk)
}
var steps int
func step(inp string) string {
steps++
// Find the first number that needs to explode.
wrk := explode(inp)
if wrk != inp {
return wrk
}
// Nothing exploded, look for a split.
return split(inp)
}
func explode(inp string) string {
var depth int
for i := range inp {
if inp[i] == '[' {
if depth == 4 {
// Explode the leftmost pair from here
}
depth++
} else if inp[i] == ']' {
depth--
}
}
}
func split(inp string) string {
return inp
}

7
2021/day18/testinput Normal file
View File

@ -0,0 +1,7 @@
[1,2]
[[1,2],3]
[9,[8,7]]
[[1,9],[8,5]]
[[[[1,2],[3,4]],[[5,6],[7,8]]],9]
[[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]]
[[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]]

View File

@ -148,7 +148,7 @@ func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
log.Fatal("Invalid Atoi:", i)
}
return ret
}