Skip to content

Commit 3272a70

Browse files
committed
Add 2024 day13 part2 solution
1 parent c518555 commit 3272a70

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

2024/day13/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ $ cargo run --release -- --input 2024/day13/input.txt 2024 13 part1
2424

2525
```bash
2626
$ cargo run --release -- --input 2024/day13/input.txt 2024 13 part2
27-
TODO
27+
85644161121698
2828
```

src/year2024/day13.rs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ops::Rem;
22

33
use anyhow::Result;
4+
use itertools::Itertools;
45
use regex::Regex;
56
use tracing::info;
67

@@ -29,6 +30,50 @@ impl Args {
2930
}
3031

3132
fn part1(input: String) -> Result<()> {
33+
let problems = parse_problems(&input);
34+
info!("problems: {problems:?}");
35+
36+
let solved: Vec<_> = problems.iter().map(|p| p.solve()).collect();
37+
info!("solved: {solved:?}");
38+
39+
let costs = (3, 1);
40+
let tokens: i64 = solved
41+
.into_iter()
42+
.flatten()
43+
.map(|(a, b)| (a * costs.0) + (b * costs.1))
44+
.sum();
45+
46+
println!("{tokens}");
47+
Ok(())
48+
}
49+
50+
fn part2(input: String) -> Result<()> {
51+
let mut problems = parse_problems(&input);
52+
info!("problems: {problems:?}");
53+
54+
let unit_conversion_error = 10000000000000;
55+
let solved: Vec<_> = problems
56+
.iter_mut()
57+
.update(|p| {
58+
p.prize.0 += unit_conversion_error;
59+
p.prize.1 += unit_conversion_error;
60+
})
61+
.map(|p| p.solve())
62+
.collect();
63+
info!("solved: {solved:?}");
64+
65+
let costs = (3, 1);
66+
let tokens: i64 = solved
67+
.into_iter()
68+
.flatten()
69+
.map(|(a, b)| (a * costs.0) + (b * costs.1))
70+
.sum();
71+
72+
println!("{tokens}");
73+
Ok(())
74+
}
75+
76+
fn parse_problems(input: &str) -> Vec<Problem> {
3277
let button_re = Regex::new(r"Button ([AB]): X\+(\d+), Y\+(\d+)").unwrap();
3378
let prize_re = Regex::new(r"Prize: X=(\d+), Y=(\d+)").unwrap();
3479

@@ -52,31 +97,14 @@ fn part1(input: String) -> Result<()> {
5297
current_problem = Problem::default();
5398
};
5499
}
55-
info!("problems: {problems:?}");
56-
57-
let solved: Vec<_> = problems.iter().map(|p| p.solve()).collect();
58-
info!("solved: {solved:?}");
59-
60-
let costs = (3, 1);
61-
let tokens: i32 = solved
62-
.into_iter()
63-
.flatten()
64-
.map(|(a, b)| (a * costs.0) + (b * costs.1))
65-
.sum();
66-
67-
println!("{tokens}");
68-
Ok(())
69-
}
70-
71-
fn part2(_input: String) -> Result<()> {
72-
Ok(())
100+
problems
73101
}
74102

75103
#[derive(Debug, Clone, Default)]
76104
struct Problem {
77-
button_a: (i32, i32),
78-
button_b: (i32, i32),
79-
prize: (i32, i32),
105+
button_a: (i64, i64),
106+
button_b: (i64, i64),
107+
prize: (i64, i64),
80108
}
81109

82110
impl Problem {
@@ -119,7 +147,7 @@ impl Problem {
119147
// a1c2(109650) - c1a2(676820) = -567170
120148
// ---------------------------- = ------- = 86 = b
121149
// a1b2(629) - b1a2(7224) = -6,595
122-
fn solve(&self) -> Option<(i32, i32)> {
150+
fn solve(&self) -> Option<(i64, i64)> {
123151
cramers_rule(
124152
(self.button_a.0, self.button_b.0, self.prize.0),
125153
(self.button_a.1, self.button_b.1, self.prize.1),
@@ -130,7 +158,7 @@ impl Problem {
130158
// In linear algebra, Cramer's rule is an explicit formula for the solution of a system of linear
131159
// equations with as many equations as unknowns, valid whenever the system has a unique solution.
132160
// https://en.wikipedia.org/wiki/Cramer%27s_rule
133-
fn cramers_rule(eq1: (i32, i32, i32), eq2: (i32, i32, i32)) -> Option<(i32, i32)> {
161+
fn cramers_rule(eq1: (i64, i64, i64), eq2: (i64, i64, i64)) -> Option<(i64, i64)> {
134162
let (a1, b1, c1) = eq1;
135163
let (a2, b2, c2) = eq2;
136164

0 commit comments

Comments
 (0)