Skip to content

Commit

Permalink
2024 - Day 14 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 14, 2024
1 parent dd689c9 commit 6b571c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
37 changes: 32 additions & 5 deletions src/main/kotlin/no/rodland/advent_2024/Day14.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,59 @@ package no.rodland.advent_2024

import no.rodland.advent.Day
import no.rodland.advent.Pos
import product

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

class Day14(val input: List<String>) : Day<Long, Long, List<Day14.Robot>> {
class Day14(val input: List<String>, val width: Int, val height: Int) : Day<Long, Long, List<Day14.Robot>> {

private val robots = input.parse()

override fun partOne(): Long {
return 2
return robots.map { it.move(100, width, height) }
.groupBy { it.quadrant(width, height) }
.toSortedMap()
.filterKeys { i: Int -> i != 0 } // middle
.values
.map { it.size }
.product()
}

override fun partTwo(): Long {
return 2
}

data class Robot(val pos: Pos, val vel: Pos) {
fun move(iterations: Int, width: Int, height: Int): Pos {
val x = (pos.x + (((iterations) * vel.x) % width) + width) % width
val y = (pos.y + (((iterations) * vel.y) % height) + height) % height
return Pos(x, y)
}
}

private fun Pos.quadrant(width: Int, height: Int): Int {
val firstX = this.x < (width / 2)
val lastX = this.x >= ((width / 2) + 1)
val firstY = this.y < (height / 2)
val lastY = this.y >= ((height / 2) + 1)
return when {
firstX && firstY -> 1
lastX && firstY -> 2
firstX && lastY -> 3
lastX && lastY -> 4
else -> 0
}
}

override fun List<String>.parse(): List<Robot> {
return map { line ->
// p=0,4 v=3,-3
val (px, py) = line.substringAfter("p=").substringBefore(" v=").split(",").map { it.toInt() }
val (vx, vy) = line.substringAfter("v=").split(",").map { it.toInt() }
Robot(Pos(px, py), Pos(vx, vy))
}
}

data class Robot(val pos: Pos, val vel: Pos)

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

16 changes: 10 additions & 6 deletions src/test/kotlin/no/rodland/advent_2024/Day14Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ internal class Day14Test {
private val data14 = "2024/input_14.txt".readFile()
private val test14 = "2024/input_14_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestOne = 12L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultOne = 221142636L
private val resultTwo = 2L
// private val width = 101
// private val height = 103
// private val widthExample = 11
// private val heightExample = 7

val test = defaultTestSuiteParseOnInit(
Day14(data14),
Day14(test14),
Day14(data14, 101, 103),
Day14(test14, 11, 7),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day14(data14) },
{ Day14(test14) },
{ Day14(data14, 101, 103) },
{ Day14(test14, 11, 7) },
)

@Nested
Expand Down

0 comments on commit 6b571c5

Please sign in to comment.