Skip to content

Commit

Permalink
2024 - Day 15 - still part 1 - but re-written to hopefully be compati…
Browse files Browse the repository at this point in the history
…ble with part 2.
  • Loading branch information
fmmr committed Dec 15, 2024
1 parent d32c497 commit c36a9cb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
90 changes: 67 additions & 23 deletions src/main/kotlin/no/rodland/advent_2024/Day15.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,86 @@ class Day15(val input: List<String>) : Day<Long, Long, Pair<Pair<Pos, Cave>, Lis
.sum().toLong()
}

private fun getRest(d: Direction, robot: Pos): List<Pos> = 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) }
override fun partTwo(): Long {
return 2
}

private fun List<Pos>.changes(cave: Cave, d: Direction, robot: Pos): Map<Pos, Char> {
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()
private fun getRest(cave: Cave, d: Direction, robot: Pos): Set<Pos> {
val set = mutableSetOf<Pos>()
val queue = ArrayDeque<Pos>()
queue.add(robot)
while (queue.isNotEmpty()) {
val p = queue.removeFirst()
when (cave[p]) {
'O', '@' -> {
set.add(p)
queue.add(p.next(d))
}

'#' -> {
return emptySet()
}

'.' -> {
set.add(p)
}

// ']' -> {
// set.add(p)
// val side = Pos(p.x - 1, p.y)
// set.add(side)
// val pnext = p.next(d)
// val sidenext = side.next(d)
// if (pnext !in queue) set.add(pnext)
// if (sidenext !in queue) set.add(sidenext)
// }
//
// '[' -> {
// set.add(p)
// val side = Pos(p.x + 1, p.y)
// set.add(side)
// val pNext = p.next(d)
// val sideNext = side.next(d)
// if (pNext !in queue) set.add(pNext)
// if (sideNext !in queue) set.add(sideNext)
// }

else -> error("Unexpected character: ${cave[p]}")
}
}
var i = 0
return (listOf('.') + (1..firstSpace).map { chars[it - 1] }).associateBy { robot.next(d, i++) }
val possible = true
return if (possible) set else emptySet()
}

private fun Set<Pos>.changes(cave: Cave, d: Direction, robot: Pos): Map<Pos, Char> {

val clearAll = map { p -> p to '.' }.toMap()
val newValues = map { p -> p.next(d) to cave[p] }.toMap()
// XXX probably something I can do here
return clearAll.mapValues { (k, v) -> newValues[k] ?: v }


// val chars = map { cave[it] }
// var i = 0
// val old = if (isEmpty()) {
// emptyMap()
// } else {
// (listOf('.') + (1..<size).map { chars[it - 1] }).associateBy { robot.next(d, i++) }
// }
// return old
}

private fun Cave.move(d: Direction, robot: Pos): Pos {
val line: List<Pos> = getRest(d, robot)
return line
.changes(this, d, robot)
val line: Set<Pos> = getRest(this, d, robot)
val changes = line.changes(this, d, robot)
return changes
.mapNotNull { (p, c) ->
this[p] = c
if (c == '@') p else null
}
.firstOrNull() ?: robot
}

override fun partTwo(): Long {
return 2
}

override fun List<String>.parse(): Pair<Pair<Pos, Cave>, List<Direction>> {
val (map, move) = joinToString("\n").split("\n\n")
var start = Pos(0, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/no/rodland/advent_2024/Day15Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class Day15Test {
resultTwo,
{ Day15(data15) },
{ Day15(test15) },
numTestPart1 = 3,
numTestPart1 = 5,
numTestPart2 = 1
)

Expand Down

0 comments on commit c36a9cb

Please sign in to comment.