Skip to content

Commit

Permalink
Merge pull request #728 from scarf005/2024/day13-fix
Browse files Browse the repository at this point in the history
docs(2024/day13): fix math display
  • Loading branch information
SethTisue authored Dec 14, 2024
2 parents 908fb44 + 72bfe27 commit 2d80fa8
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions docs/2024/puzzles/day13.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,54 +81,54 @@ We need another approach. Let's look at the condition carefully...

Turns out we can express it using system of equations! For number of button presses `A` and `B`, our target `x` and `y` can be described as following equation:

```math
$$
\begin{cases}
A \cdot ax + B \cdot bx = x \\
A \cdot ay + B \cdot by = y
\end{cases}
```
$$

Which can be solved for `A` in terms of `B`:

```math
$$
A = \frac{x - B \cdot bx}{ax}, \quad A = \frac{y - B \cdot by}{ay}
```
$$

Then `A` can be equated in both expressions:

```math
$$
\frac{x - B \cdot bx}{ax} = \frac{y - B \cdot by}{ay}
```
$$

Now `ax` and `ay` can be cross-multiplied to eliminate denominators:

```math
$$
(x - B \cdot bx) \cdot ay = (y - B \cdot by) \cdot ax
```
$$

...Expand and rearrange:

```math
$$
x \cdot ay - B \cdot bx \cdot ay = y \cdot ax - B \cdot by \cdot ax
```
$$

Group terms involving `B`:

```math
$$
x \cdot ay - y \cdot ax = B \cdot (bx \cdot ay - by \cdot ax)
```
$$

Then solve for `B`:

```math
$$
B = \frac{x \cdot ay - y \cdot ax}{bx \cdot ay - by \cdot ax}
```
$$

Using `B`, `A` can also be solved:

```math
$$
A = \frac{x - B \cdot bx}{ax}
```
$$

There's two more important requirement for `A` and `B`:
1. `A` and `B` should both be an natural number.
Expand Down 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 2d80fa8

Please sign in to comment.