adventofcode/2022/day17/main.go

70 lines
1.6 KiB
Go
Raw Permalink Normal View History

2022-12-20 21:04:26 +00:00
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToString()
2022-12-27 20:59:52 +00:00
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}},
2022-12-20 21:04:26 +00:00
}
2022-12-27 20:59:52 +00:00
grid := map[h.Coordinate]struct{}{}
move := func(rock []h.Coordinate, delta h.Coordinate) bool {
nrock := make([]h.Coordinate, len(rock))
2022-12-27 20:55:45 +00:00
for i, p := range rock {
p = p.Add(delta)
if _, ok := grid[p]; ok || p.X < 0 || p.X >= 7 || p.Y < 0 {
2022-12-20 21:04:26 +00:00
return false
}
2022-12-27 20:55:45 +00:00
nrock[i] = p
2022-12-20 21:04:26 +00:00
}
2022-12-27 20:55:45 +00:00
copy(rock, nrock)
2022-12-20 21:04:26 +00:00
return true
}
2022-12-27 20:55:45 +00:00
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()
2022-12-20 21:04:26 +00:00
}
2022-12-27 20:55:45 +00:00
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
}
2022-12-20 21:04:26 +00:00
}
2022-12-27 20:55:45 +00:00
cache[k] = []int{i, height}
2022-12-20 21:04:26 +00:00
2022-12-27 20:59:52 +00:00
rock := []h.Coordinate{}
2022-12-27 20:55:45 +00:00
for _, p := range rocks[i%len(rocks)] {
2022-12-27 20:59:52 +00:00
rock = append(rock, p.Add(h.Coordinate{X: 2, Y: height + 3}))
2022-12-20 21:04:26 +00:00
}
2022-12-27 20:55:45 +00:00
for {
2022-12-27 20:59:52 +00:00
move(rock, h.Coordinate{X: int(inp[jet]) - int('='), Y: 0})
2022-12-27 20:55:45 +00:00
jet = (jet + 1) % len(inp)
2022-12-27 20:59:52 +00:00
if !move(rock, h.Coordinate{X: 0, Y: -1}) {
2022-12-27 20:55:45 +00:00
for _, p := range rock {
grid[p] = struct{}{}
if p.Y+1 > height {
height = p.Y + 1
}
}
break
}
2022-12-20 21:04:26 +00:00
}
}
}