adventofcode/2022/day17/main.go
2022-12-27 14:59:52 -06:00

70 lines
1.6 KiB
Go

package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToString()
rocks := [][]h.Coordinate{
{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 2, Y: 0}, {X: 3, Y: 0}},
{{X: 1, Y: 2}, {X: 0, Y: 1}, {X: 1, Y: 1}, {X: 2, Y: 1}, {X: 1, Y: 0}},
{{X: 2, Y: 2}, {X: 2, Y: 1}, {X: 0, Y: 0}, {X: 1, Y: 0}, {X: 2, Y: 0}},
{{X: 0, Y: 3}, {X: 0, Y: 2}, {X: 0, Y: 1}, {X: 0, Y: 0}},
{{X: 0, Y: 1}, {X: 1, Y: 1}, {X: 0, Y: 0}, {X: 1, Y: 0}},
}
grid := map[h.Coordinate]struct{}{}
move := func(rock []h.Coordinate, delta h.Coordinate) bool {
nrock := make([]h.Coordinate, len(rock))
for i, p := range rock {
p = p.Add(delta)
if _, ok := grid[p]; ok || p.X < 0 || p.X >= 7 || p.Y < 0 {
return false
}
nrock[i] = p
}
copy(rock, nrock)
return true
}
cache := map[[2]int][]int{}
height, jet := 0, 0
for i := 0; i < 1000000000000; i++ {
if i == 2022 {
fmt.Println("# Part 1")
fmt.Println(height)
fmt.Println()
}
k := [2]int{i % len(rocks), jet}
if c, ok := cache[k]; ok {
if n, d := 1000000000000-i, i-c[0]; n%d == 0 {
fmt.Println("# Part 2")
fmt.Println(height + n/d*(height-c[1]))
break
}
}
cache[k] = []int{i, height}
rock := []h.Coordinate{}
for _, p := range rocks[i%len(rocks)] {
rock = append(rock, p.Add(h.Coordinate{X: 2, Y: height + 3}))
}
for {
move(rock, h.Coordinate{X: int(inp[jet]) - int('='), Y: 0})
jet = (jet + 1) % len(inp)
if !move(rock, h.Coordinate{X: 0, Y: -1}) {
for _, p := range rock {
grid[p] = struct{}{}
if p.Y+1 > height {
height = p.Y + 1
}
}
break
}
}
}
}