Skip to content

Commit

Permalink
2024: day 14 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
plagov committed Dec 23, 2024
1 parent 369c66a commit 657e373
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/kotlin/io/plagov/advent/aoc2024/Day14.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.plagov.advent.aoc2024

import java.io.File
import kotlin.math.abs

class Day14 {
Expand Down Expand Up @@ -27,6 +28,36 @@ class Day14 {
return topLeft * bottomLeft * topRight * bottomRight
}

fun partTwo(input: List<String>): Int {
val robots = input.map { parseInput(it) }
var seconds = 0

while (true) {
seconds++
for (robot in robots) {
move(robot)
}
val array = populateArray(robots)
if (hasChristmasTree(array)) {
break
}
}

return seconds
}

private fun hasChristmasTree(array: Array<CharArray>): Boolean {
return array.any { it.joinToString(",").contains("#,#,#,#,#,#,#,#,#,#") }
}

private fun populateArray(robots: List<Robot>): Array<CharArray> {
val array = Array(rowCount) { CharArray(colCount) { '.' } }
robots.forEach { robot ->
array[robot.pRow][robot.pCol] = '#'
}
return array
}

private fun move(robot: Robot) {
val rowMove = robot.pRow + robot.vRow
val colMove = robot.pCol + robot.vCol
Expand Down
5 changes: 5 additions & 0 deletions src/test/kotlin/io/plagov/advent/aoc2024/Day14Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ class Day14Test {
fun partOne() {
Assertions.assertEquals(216027840, day14.partOne(input))
}

@Test
fun partTwo() {
Assertions.assertEquals(6876, day14.partTwo(input))
}
}

0 comments on commit 657e373

Please sign in to comment.