-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
22 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters