Skip to content

Commit

Permalink
refactor: reorder code
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 14, 2024
1 parent b67cca6 commit 72bfe27
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions docs/2024/puzzles/day13.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,15 @@ def part2(input: String): Long =
.sum
```

## Final Solution
## Final Code

```scala
extension (a: Long)
infix def safeDiv(b: Long): Option[Long] =
Option.when(b != 0 && a % b == 0)(a / b)

case class Claw(ax: Long, ay: Long, bx: Long, by: Long, x: Long, y: Long):
def solve: Option[Long] = for
b <- (x * ay - y * ax) safeDiv (bx * ay - by * ax)
a <- (x - b * bx) safeDiv ax
yield a * 3 + b

object L:
def unapply(s: String): Option[Long] = s.toLongOption

object Claw:
def parse(xs: Seq[String]): Option[Claw] = xs match
case Seq(
Expand All @@ -194,6 +187,13 @@ object Claw:
def parse(input: String): Seq[Claw] =
input.split("\n+").toSeq.grouped(3).flatMap(Claw.parse).toSeq

extension (a: Long)
infix def safeDiv(b: Long): Option[Long] =
Option.when(b != 0 && a % b == 0)(a / b)

object L:
def unapply(s: String): Option[Long] = s.toLongOption

def part1(input: String): Long =
parse(input).flatMap(_.solve).sum

Expand Down

0 comments on commit 72bfe27

Please sign in to comment.