Skip to content

Commit 4ca1b97

Browse files
committed
Year 2024 Day 13
1 parent 8083764 commit 4ca1b97

File tree

7 files changed

+88
-4
lines changed

7 files changed

+88
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8484
| 10 | [Hoof It](https://adventofcode.com/2024/day/10) | [Source](src/year2024/day10.rs) | 38 |
8585
| 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [Source](src/year2024/day11.rs) | 248 |
8686
| 12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [Source](src/year2024/day12.rs) | 289 |
87+
| 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [Source](src/year2024/day13.rs) | 14 |
8788

8889
## 2023
8990

benches/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ benchmark!(year2023
8787
);
8888

8989
benchmark!(year2024
90-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12
90+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
9191
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ library!(year2023 "Restore global snow production."
6767
);
6868

6969
library!(year2024 "Locate the Chief Historian in time for the big Christmas sleigh launch."
70-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12
70+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
7171
);

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,5 @@ run!(year2023
139139
);
140140

141141
run!(year2024
142-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12
142+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
143143
);

src/year2024/day13.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! # Claw Contraption
2+
//!
3+
//! Each claw machine is a system of two linear equations:
4+
//!
5+
//! ```none
6+
//! (Button A X) * (A presses) + (Button B X) * (B presses) = Prize X
7+
//! (Button A Y) * (A presses) + (Button B Y) * (B presses) = Prize Y
8+
//! ```
9+
//!
10+
//! Shortening the names and representing as a matrix:
11+
//!
12+
//! ```none
13+
//! [ ax bx ][ a ] = [ px ]
14+
//! [ ay by ][ b ] = [ py ]
15+
//! ```
16+
//!
17+
//! To solve we invert the 2 x 2 matrix then premultiply the right column.
18+
use crate::util::parse::*;
19+
use crate::util::iter::*;
20+
21+
type Claw = [i64; 6];
22+
23+
pub fn parse(input: &str) -> Vec<Claw> {
24+
input.iter_signed().chunk::<6>().collect()
25+
}
26+
27+
pub fn part1(input: &[Claw]) -> i64 {
28+
input.iter().map(|row| play(row, 0)).sum()
29+
}
30+
31+
pub fn part2(input: &[Claw]) -> i64 {
32+
input.iter().map(|row| play(row, 10_000_000_000_000)).sum()
33+
}
34+
35+
/// Invert the 2 x 2 matrix representing the system of linear equations.
36+
fn play(&[ax, ay, bx, by, px, py]: &Claw, extra: i64) -> i64 {
37+
let px = px + extra;
38+
let py = py + extra;
39+
let det = ax * by - ay * bx;
40+
41+
// If determinant is zero there is no solution.
42+
if det != 0 {
43+
let a = by * px - bx * py;
44+
let b = ax * py - ay * px;
45+
46+
// Integer solutions only.
47+
if a % det == 0 && b % det == 0 {
48+
return 3 * (a / det) + (b / det);
49+
}
50+
}
51+
52+
0
53+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ test!(year2023
8080
);
8181

8282
test!(year2024
83-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12
83+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
8484
);

tests/year2024/day13.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use aoc::year2024::day13::*;
2+
3+
const EXAMPLE: &str = "\
4+
Button A: X+94, Y+34
5+
Button B: X+22, Y+67
6+
Prize: X=8400, Y=5400
7+
8+
Button A: X+26, Y+66
9+
Button B: X+67, Y+21
10+
Prize: X=12748, Y=12176
11+
12+
Button A: X+17, Y+86
13+
Button B: X+84, Y+37
14+
Prize: X=7870, Y=6450
15+
16+
Button A: X+69, Y+23
17+
Button B: X+27, Y+71
18+
Prize: X=18641, Y=10279";
19+
20+
#[test]
21+
fn part1_test() {
22+
let input = parse(EXAMPLE);
23+
assert_eq!(part1(&input), 480);
24+
}
25+
26+
#[test]
27+
fn part2_test() {
28+
let input = parse(EXAMPLE);
29+
assert_eq!(part2(&input), 875318608908);
30+
}

0 commit comments

Comments
 (0)