2021 Day 18 Part 2
This commit is contained in:
parent
dd306d2966
commit
e77a316ee6
1 changed files with 26 additions and 0 deletions
|
|
@ -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())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue