Watch
1
0
Fork
You've already forked advent-of-code
0

2021 Day 18 Part 2

This commit is contained in:
Jeremy Kaplan 2021-12-18 01:08:24 -08:00
commit e77a316ee6

View file

@ -9,6 +9,7 @@ import (
func main() { func main() {
lines := aoc.Input().ReadLines("day18.txt") lines := aoc.Input().ReadLines("day18.txt")
fmt.Println(part1(lines)) fmt.Println(part1(lines))
fmt.Println(part2(lines))
} }
func part1(lines []string) int { func part1(lines []string) int {
@ -25,6 +26,31 @@ func part1(lines []string) int {
return n.Magnitude() return n.Magnitude()
} }
func part2(lines []string) int {
var pairs [][2]string
for i := 0; i < len(lines)-1; i++ {
for j := i + 1; j < len(lines); j++ {
pairs = append(pairs,
[2]string{lines[i], lines[j]},
[2]string{lines[j], lines[i]},
)
}
}
max := 0
for _, p := range pairs {
a, b := Tree(p[0]), Tree(p[1])
n := add(a, b)
for n.Reduce() {
}
m := n.Magnitude()
if m > max {
max = m
}
}
return max
}
func add(a, b *Node) *Node { func add(a, b *Node) *Node {
n := Pair(a, b) n := Pair(a, b)
link(a.B().Right(), b.A().Left()) link(a.B().Right(), b.A().Left())