Day 14.
This commit is contained in:
parent
3b1410e2fd
commit
226e61a203
185
src/aoc2023/Day14.kt
Normal file
185
src/aoc2023/Day14.kt
Normal file
@ -0,0 +1,185 @@
|
||||
package aoc2023
|
||||
|
||||
import CharGrid
|
||||
import println
|
||||
import readInput
|
||||
|
||||
/*
|
||||
--- Day 14: Parabolic Reflector Dish ---
|
||||
You reach the place where all of the mirrors were pointing: a massive parabolic reflector dish attached to the side of another large mountain.
|
||||
The dish is made up of many small mirrors, but while the mirrors themselves are roughly in the shape of a parabolic reflector dish, each individual mirror seems to be pointing in slightly the wrong direction. If the dish is meant to focus light, all it's doing right now is sending it in a vague direction.
|
||||
This system must be what provides the energy for the lava! If you focus the reflector dish, maybe you can go where it's pointing and use the light to fix the lava production.
|
||||
Upon closer inspection, the individual mirrors each appear to be connected via an elaborate system of ropes and pulleys to a large metal platform below the dish. The platform is covered in large rocks of various shapes. Depending on their position, the weight of the rocks deforms the platform, and the shape of the platform controls which ropes move and ultimately the focus of the dish.
|
||||
In short: if you move the rocks, you can focus the dish. The platform even has a control panel on the side that lets you tilt it in one of four directions! The rounded rocks (O) will roll when the platform is tilted, while the cube-shaped rocks (#) will stay in place. You note the positions of all of the empty spaces (.) and rocks (your puzzle input). For example:
|
||||
O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....
|
||||
|
||||
Start by tilting the lever so all of the rocks will slide north as far as they will go:
|
||||
OOOO.#.O..
|
||||
OO..#....#
|
||||
OO..O##..O
|
||||
O..#.OO...
|
||||
........#.
|
||||
..#....#.#
|
||||
..O..#.O.O
|
||||
..O.......
|
||||
#....###..
|
||||
#....#....
|
||||
|
||||
You notice that the support beams along the north side of the platform are damaged; to ensure the platform doesn't collapse, you should calculate the total load on the north support beams.
|
||||
The amount of load caused by a single rounded rock (O) is equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on. (Cube-shaped rocks (#) don't contribute to load.) So, the amount of load caused by each rock in each row is as follows:
|
||||
OOOO.#.O.. 10
|
||||
OO..#....# 9
|
||||
OO..O##..O 8
|
||||
O..#.OO... 7
|
||||
........#. 6
|
||||
..#....#.# 5
|
||||
..O..#.O.O 4
|
||||
..O....... 3
|
||||
#....###.. 2
|
||||
#....#.... 1
|
||||
|
||||
The total load is the sum of the load caused by all of the rounded rocks. In this example, the total load is 136.
|
||||
Tilt the platform so that the rounded rocks all roll north. Afterward, what is the total load on the north support beams?
|
||||
|
||||
*/
|
||||
fun main() {
|
||||
|
||||
val inlineTestInput = """
|
||||
O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....
|
||||
"""
|
||||
|
||||
fun part1(input: List<String>): Int {
|
||||
val grid = CharGrid(input, borderChar = '#')
|
||||
for (r in 0 until grid.height) {
|
||||
for (c in 0 until grid.width) {
|
||||
var rr = r
|
||||
while (grid[c, rr] == '.' && grid[c, rr + 1] == 'O') {
|
||||
grid[c, rr] = 'O'
|
||||
grid[c, rr + 1] = '.'
|
||||
rr--
|
||||
}
|
||||
}
|
||||
}
|
||||
var sum = 0
|
||||
for (r in 0 until grid.height) {
|
||||
for (c in 0 until grid.width) {
|
||||
if (grid[c, r] == 'O') {
|
||||
sum += grid.height - r
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
fun part2(input: List<String>): Int {
|
||||
val grid = CharGrid(input, borderChar = '#')
|
||||
var cycleCount = 0
|
||||
val memo = HashMap<CharGrid, MutableSet<Int>>()
|
||||
var trackmode = false
|
||||
var cycleOffset = 0
|
||||
val cycle = ArrayList<CharGrid>()
|
||||
do {
|
||||
for (r in 0 until grid.height) {
|
||||
for (c in 0 until grid.width) {
|
||||
var rr = r
|
||||
while (grid[c, rr] == '.' && grid[c, rr + 1] == 'O') {
|
||||
grid[c, rr] = 'O'
|
||||
grid[c, rr + 1] = '.'
|
||||
rr--
|
||||
}
|
||||
}
|
||||
}
|
||||
for (c in 0 until grid.width) {
|
||||
for (r in 0 until grid.height) {
|
||||
var cc = c
|
||||
while (grid[cc, r] == '.' && grid[cc + 1, r] == 'O') {
|
||||
grid[cc, r] = 'O'
|
||||
grid[cc + 1, r] = '.'
|
||||
cc--
|
||||
}
|
||||
}
|
||||
}
|
||||
for (r in grid.height - 1 downTo 0) {
|
||||
for (c in 0 until grid.width) {
|
||||
var rr = r
|
||||
while (grid[c, rr] == '.' && grid[c, rr - 1] == 'O') {
|
||||
grid[c, rr] = 'O'
|
||||
grid[c, rr - 1] = '.'
|
||||
rr++
|
||||
}
|
||||
}
|
||||
}
|
||||
for (c in grid.width - 1 downTo 0) {
|
||||
for (r in 0 until grid.height) {
|
||||
var cc = c
|
||||
while (grid[cc, r] == '.' && grid[cc - 1, r] == 'O') {
|
||||
grid[cc, r] = 'O'
|
||||
grid[cc - 1, r] = '.'
|
||||
cc++
|
||||
}
|
||||
}
|
||||
}
|
||||
val set = memo.getOrPut(grid.copyOf()) { HashSet() }
|
||||
if (set.isNotEmpty()) {
|
||||
if (trackmode == false) {
|
||||
cycleOffset = cycleCount
|
||||
}
|
||||
trackmode = true
|
||||
}
|
||||
if (set.size == 2) break
|
||||
if (trackmode) {
|
||||
cycle.add(grid.copyOf())
|
||||
}
|
||||
cycleCount++
|
||||
set.add(cycleCount)
|
||||
|
||||
} while (true)
|
||||
|
||||
println("cycleCount $cycleCount, cycleOffset $cycleOffset, cycleLength ${cycle.size}")
|
||||
val offset = (1_000_000_000 - cycleOffset - 1) % cycle.size
|
||||
println("offset $offset")
|
||||
val pickGrid = cycle[offset]
|
||||
println(cycleCount)
|
||||
var sum = 0
|
||||
for (r in 0 until pickGrid.height) {
|
||||
for (c in 0 until pickGrid.width) {
|
||||
if (pickGrid[c, r] == 'O') {
|
||||
sum += pickGrid.height - r
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
// test if implementation meets criteria from the description, like:
|
||||
val testInput = inlineTestInput.trim().reader().readLines()
|
||||
//val testInput = readInput("aoc2023/Day14_test")
|
||||
val testInputPart1Result = part1(testInput)
|
||||
println("Part 1 Test: $testInputPart1Result")
|
||||
val testInputPart2Result = part2(testInput)
|
||||
println("Part 2 Test: $testInputPart2Result")
|
||||
check(testInputPart1Result == 136)
|
||||
check(testInputPart2Result == 64)
|
||||
|
||||
val input = readInput("aoc2023/Day14")
|
||||
part1(input).println()
|
||||
part2(input).println()
|
||||
}
|
Loading…
Reference in New Issue
Block a user