Skip to content

Commit

Permalink
Solve 2024 day 13 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 13, 2024
1 parent 43af2c9 commit 0df8f3c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
70 changes: 46 additions & 24 deletions src/main/scala/eu/sim642/adventofcode2024/Day13.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,73 @@ package eu.sim642.adventofcode2024

import eu.sim642.adventofcodelib.graph.{Dijkstra, GraphSearch, TargetNode}
import eu.sim642.adventofcodelib.pos.Pos
import eu.sim642.adventofcodelib.IntegralImplicits._
import eu.sim642.adventofcodelib.IntegralImplicits.*
import eu.sim642.adventofcodelib.NumberTheory

object Day13 {

case class ClawMachine(buttonA: Pos, buttonB: Pos, prize: Pos)
case class LongPos(x: Long, y: Long)

def minWinTokens(clawMachine: ClawMachine): Int = {
val ClawMachine(buttonA, buttonB, prize) = clawMachine
case class ClawMachine(buttonA: Pos, buttonB: Pos, prize: LongPos)

/*val graphSearch = new GraphSearch[Pos] with TargetNode[Pos] {
override val startNode: Pos = Pos.zero
trait Part {
def minWinTokens(clawMachine: ClawMachine): Long

override def neighbors(pos: Pos): IterableOnce[(Pos, Int)] = {
for {
(button, tokens) <- Seq(clawMachine.buttonA -> 3, clawMachine.buttonB -> 1)
newPos = pos + button
if newPos <= targetNode
} yield newPos -> tokens
def sumMinWinTokens(clawMachines: Seq[ClawMachine]): Long = clawMachines.map(minWinTokens).sum
}

object Part1 extends Part {
override def minWinTokens(clawMachine: ClawMachine): Long = {
val ClawMachine(buttonA, buttonB, prize) = clawMachine

/*val graphSearch = new GraphSearch[Pos] with TargetNode[Pos] {
override val startNode: Pos = Pos.zero
override def neighbors(pos: Pos): IterableOnce[(Pos, Int)] = {
for {
(button, tokens) <- Seq(clawMachine.buttonA -> 3, clawMachine.buttonB -> 1)
newPos = pos + button
if newPos <= targetNode
} yield newPos -> tokens
}
override val targetNode: Pos = clawMachine.prize
}
override val targetNode: Pos = clawMachine.prize
}
Dijkstra.search(graphSearch).target.map(_._2).getOrElse(0)*/

Dijkstra.search(graphSearch).target.map(_._2).getOrElse(0)*/
/*(for {
timesA <- 0 to 100
prizeB = prize - timesA *: buttonA
timesB <- prizeB.x /! buttonB.x
if timesB * buttonB.y == prizeB.y
} yield 3 * timesA + timesB).minOption.getOrElse(0)*/

(for {
timesA <- 0 to 100
prizeB = prize - timesA *: buttonA
timesB <- prizeB.x /! buttonB.x
if timesB * buttonB.y == prizeB.y
} yield 3 * timesA + timesB).minOption.getOrElse(0)
(for {
timesB <- (prize.x * buttonA.y - prize.y * buttonA.x) /! (buttonB.x * buttonA.y - buttonB.y * buttonA.x)
timesA <- (prize.x - timesB * buttonB.x) /! buttonA.x
} yield 3 * timesA + timesB).getOrElse(0)
}
}

def sumMinWinTokens(clawMachines: Seq[ClawMachine]): Int = clawMachines.map(minWinTokens).sum
object Part2 extends Part {
override def minWinTokens(clawMachine: ClawMachine): Long = {
val newClawMachine = clawMachine.copy(prize = LongPos(clawMachine.prize.x + 10000000000000L, clawMachine.prize.y + 10000000000000L))
Part1.minWinTokens(newClawMachine)
}
}

def parseClawMachine(s: String): ClawMachine = s match {
case s"Button A: X+$aX, Y+$aY\nButton B: X+$bX, Y+$bY\nPrize: X=$pX, Y=$pY" =>
ClawMachine(Pos(aX.toInt, aY.toInt), Pos(bX.toInt, bY.toInt), Pos(pX.toInt, pY.toInt))
ClawMachine(Pos(aX.toInt, aY.toInt), Pos(bX.toInt, bY.toInt), LongPos(pX.toLong, pY.toLong))
}

def parseClawMachines(input: String): Seq[ClawMachine] = input.split("\n\n").map(parseClawMachine).toSeq

lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day13.txt")).mkString.trim

def main(args: Array[String]): Unit = {
println(sumMinWinTokens(parseClawMachines(input)))
println(Part1.sumMinWinTokens(parseClawMachines(input)))
println(Part2.sumMinWinTokens(parseClawMachines(input)))
}
}
12 changes: 10 additions & 2 deletions src/test/scala/eu/sim642/adventofcode2024/Day13Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ class Day13Test extends AnyFunSuite {
|Prize: X=18641, Y=10279""".stripMargin

test("Part 1 examples") {
assert(sumMinWinTokens(parseClawMachines(exampleInput)) == 480)
assert(Part1.sumMinWinTokens(parseClawMachines(exampleInput)) == 480)
}

test("Part 1 input answer") {
assert(sumMinWinTokens(parseClawMachines(input)) == 31623)
assert(Part1.sumMinWinTokens(parseClawMachines(input)) == 31623)
}

test("Part 2 examples") {
assert(Part2.sumMinWinTokens(parseClawMachines(exampleInput)) == 875318608908L) // from IRC
}

test("Part 2 input answer") {
assert(Part2.sumMinWinTokens(parseClawMachines(input)) == 93209116744825L)
}
}

0 comments on commit 0df8f3c

Please sign in to comment.