Skip to content

Commit

Permalink
2024 - Day 13 - added some comments on the equations
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 13, 2024
1 parent 78ff5c6 commit d936a59
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/kotlin/no/rodland/advent_2024/Day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ class Day13(val input: List<String>) : Day<Long, Long, List<Day13.Machine>> {
}

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
// Button A: X+94, Y+34
// Button B: X+22, Y+67
// Prize: X=8400, Y=5400

// 1: a.x*ca + b.x*cb = p.x
// 2: a.y*ca + b.y*cb = p.y

// 1: ca = (p.x - b.x*cb) / a.x
// 2+1: a.y*((p.x - b.x*cb) / a.x) + b.y*cb = p.y
// b.y*cb - a.y/a.x * b.x*cb = p.y - a.y/a.x * p.x
// cb * (b.y - a.y/a.x * b.x) = p.y - a.y/a.x * p.x
// cb = (p.y - a.y/a.x * p.x) / (b.y - a.y/a.x * b.x)
// cb = (a.x * p.y - a.y * p.x) / (a.x * b.y - a.y * b.x)

val cb = (a.x * p.y - a.y * p.x) / (a.x * b.y - a.y * b.x)
val ca = (p.x - b.x * cb) / a.x
val ok = (cb * b.x + ca * a.x == p.x) && (cb * b.y + ca * a.y == p.y)
}

Expand Down

0 comments on commit d936a59

Please sign in to comment.