Skip to content

Commit 13b206d

Browse files
committed
Year 2024 Day 13
1 parent 8083764 commit 13b206d

File tree

7 files changed

+99
-4
lines changed

7 files changed

+99
-4
lines changed

README.md

+1
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

+1-1
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

+1-1
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

+1-1
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

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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, false)).sum()
29+
}
30+
31+
pub fn part2(input: &[Claw]) -> i64 {
32+
input.iter().map(|row| play(row, true)).sum()
33+
}
34+
35+
/// Invert the 2 x 2 matrix representing the system of linear equations.
36+
fn play(&[ax, ay, bx, by, mut px, mut py]: &Claw, part_two: bool) -> i64 {
37+
if part_two {
38+
px += 10_000_000_000_000;
39+
py += 10_000_000_000_000;
40+
}
41+
42+
// If determinant is zero there's no solution.
43+
let det = ax * by - ay * bx;
44+
if det == 0 {
45+
return 0;
46+
}
47+
48+
let mut a = by * px - bx * py;
49+
let mut b = ax * py - ay * px;
50+
51+
// Integer solutions only.
52+
if a % det != 0 || b % det != 0 {
53+
return 0;
54+
}
55+
56+
a /= det;
57+
b /= det;
58+
59+
if part_two || (a <= 100 && b <= 100) {
60+
3 * a + b
61+
} else {
62+
0
63+
}
64+
}

tests/test.rs

+1-1
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

+30
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)