Skip to content

Commit

Permalink
2024 - Day 13 - part 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 13, 2024
1 parent 9e91dab commit 78ff5c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
29 changes: 18 additions & 11 deletions src/main/kotlin/no/rodland/advent_2024/Day13.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
package no.rodland.advent_2024

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

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

class Day13(val input: List<String>) : Day<Long, Long, List<Day13.Machine>> {

private val machines = input.parse()
private val machinesPart2 = machines.map { it.copy(p = P(10000000000000L + it.p.x, 10000000000000L + it.p.y)) }

override fun partOne(): Long {
return 2
return machines.filter { it.ok }.sumOf { it.ca * 3 + it.cb }
}

override fun partTwo(): Long {
return 2
return machinesPart2.filter { it.ok }.sumOf { it.ca * 3 + it.cb }
}

data class Machine(val a: P, val b: P, val p: P) {
val cb = (p.y * a.x - p.x * a.y) / (a.x * b.y - a.y * b.x)
val ca = (p.x - cb * b.x) / a.x
val ok = (cb * b.x + ca * a.x == p.x) && (cb * b.y + ca * a.y == p.y)
}

override fun List<String>.parse(): List<Machine> {
return chunked(4).map { (a, b, p) ->
val ax = a.substringAfter("X+").substringBefore(",").toInt()
val ay = a.substringAfter("Y+").toInt()
val bx = b.substringAfter("X+").substringBefore(",").toInt()
val by = b.substringAfter("Y+").toInt()
val px = p.substringAfter("X=").substringBefore(",").toInt()
val py = p.substringAfter("Y=").toInt()
Machine(Pos(ax, ay), Pos(bx, by), Pos(px, py))
val ax = a.substringAfter("X+").substringBefore(",").toLong()
val ay = a.substringAfter("Y+").toLong()
val bx = b.substringAfter("X+").substringBefore(",").toLong()
val by = b.substringAfter("Y+").toLong()
val px = p.substringAfter("X=").substringBefore(",").toLong()
val py = p.substringAfter("Y=").toLong()
Machine(P(ax, ay), P(bx, by), P(px, py))
}
}

data class Machine(val a: Pos, val b: Pos, val prize: Pos)
data class P(val x: Long, val y: Long)

override val day = "13".toInt()
}
8 changes: 4 additions & 4 deletions src/test/kotlin/no/rodland/advent_2024/Day13Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal class Day13Test {
private val data13 = "2024/input_13.txt".readFile()
private val test13 = "2024/input_13_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultTwo = 2L
private val resultTestOne = 480L
private val resultTestTwo = 875318608908L
private val resultOne = 36954L
private val resultTwo = 79352015273424L

val test = defaultTestSuiteParseOnInit(
Day13(data13),
Expand Down

0 comments on commit 78ff5c6

Please sign in to comment.