Skip to content

Commit

Permalink
2024 - Day 15 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 15, 2024
1 parent fab1c4c commit 9bf3afb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
76 changes: 65 additions & 11 deletions src/main/kotlin/no/rodland/advent_2024/Day15.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,88 @@
package no.rodland.advent_2024

import no.rodland.advent.Cave
import no.rodland.advent.Day
import no.rodland.advent.Direction
import no.rodland.advent.Direction.Companion.fromChar
import no.rodland.advent.*

// template generated: 15/12/2024
// Fredrik Rødland 2024

class Day15(val input: List<String>) : Day<Long, Long, Pair<Array<CharArray>, List<Direction>>> {
class Day15(val input: List<String>) : Day<Long, Long, Pair<Pair<Pos, Cave>, List<Direction>>> {

private val parsed = input.parse()
private val grid = parsed.first
private val movements = parsed.second
private val start = parsed.first.first
private val grid = parsed.first.second
private val directions = parsed.second
private val width = grid[0].size
private val height = grid.size

override fun partOne(): Long {
return 2
val (endGrid, robot) = directions.fold(grid to start) { g, d ->
g.first.move(d, g.second)
}
// endGrid.print()
return endGrid.flatMapIndexed { y, row ->
row.mapIndexed { x, c ->
if (c == 'O') {
100 * y + x
} else {
0
}

}
}.sum().toLong()
}

private fun getRest(d: Direction, robot: Pos): List<Pos> {
return when (d) {
Direction.NORTH -> ((robot.y) downTo 0).map { Pos(robot.x, it) }
Direction.SOUTH -> ((robot.y)..<height).map { Pos(robot.x, it) }
Direction.WEST -> ((robot.x) downTo 0).map { Pos(it, robot.y) }
Direction.EAST -> ((robot.x)..<width).map { Pos(it, robot.y) }
}
}

private fun List<Pos>.fix(cave: Cave, d: Direction, robot: Pos): Map<Pos, Char> {
// cannot move walls

val chars = map { cave[it] }
val firstSpace = chars.indexOf('.')
val firstWall = chars.indexOf('#')
if (firstWall > -1 && firstWall < firstSpace) {
return emptyMap()
}
if (firstSpace == -1) {
return emptyMap()
}
var i = 0
return (listOf('.') + (1..firstSpace).map { chars[it - 1] }).map { robot.next(d, i++) to it }.toMap()
}

private fun Cave.move(d: Direction, robot: Pos): Pair<Cave, Pos> {
val line: List<Pos> = getRest(d, robot)
val fixedLine = line.fix(this, d, robot)
var newRobot = Pos(0, 0)
val newGrid = indices.map { y ->
indices.map { x ->
(fixedLine[Pos(x, y)] ?: get(Pos(x, y))).also { if (it == '@') newRobot = Pos(x, y) }

}.toCharArray()
}.toTypedArray<CharArray>()
return newGrid to newRobot
}

override fun partTwo(): Long {
return 2
}

override fun List<String>.parse(): Pair<Array<CharArray>, List<Direction>> {
override fun List<String>.parse(): Pair<Pair<Pos, Cave>, List<Direction>> {
val (map, move) = joinToString("\n").split("\n\n")
var start = Pos(0, 0)
val lines = map.lines()
val cave = lines.indices.map { y -> lines.indices.map { x -> lines[y][x] }.toCharArray() }.toTypedArray<CharArray>()
val cave = lines.indices.map { y -> lines.indices.map { x -> lines[y][x].also { if (it == '@') start = Pos(x, y) } }.toCharArray() }.toTypedArray<CharArray>()
val directions = move.split("\n").flatMap { s -> s.map { Direction.fromChar(it) } }
return cave to directions
return (start to cave) to directions
}

override val day = "15".toInt()
}


7 changes: 5 additions & 2 deletions src/test/kotlin/no/rodland/advent_2024/Day15Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal class Day15Test {
private val data15 = "2024/input_15.txt".readFile()
private val test15 = "2024/input_15_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestOne = 10092L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultOne = 1552879L
private val resultTwo = 2L

val test = defaultTestSuiteParseOnInit(
Expand All @@ -29,6 +29,8 @@ internal class Day15Test {
resultTwo,
{ Day15(data15) },
{ Day15(test15) },
numTestPart1 = 1,
numTestPart2 = 1
)

@Nested
Expand Down Expand Up @@ -62,6 +64,7 @@ internal class Day15Test {
}

@Test
@Slow(600)
fun `15,1,live,1`() {
report(test.livePart1)
}
Expand Down

0 comments on commit 9bf3afb

Please sign in to comment.