-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.rs
165 lines (139 loc) · 4.08 KB
/
day13.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::solutions::Solution;
use crate::utils::point::Point;
use itertools::Itertools;
use std::str::FromStr;
pub struct Day13;
impl Solution for Day13 {
fn part_one(&self, input: &str) -> String {
self.parse(input)
.iter()
.filter_map(|machine| {
machine
.solve_2x2_system()
.map(|solution| solution.0 * 3 + solution.1)
})
.sum::<usize>()
.to_string()
}
fn part_two(&self, input: &str) -> String {
self.parse(input)
.iter()
.filter_map(|machine| {
machine
.move_prize_by_10000000000000()
.solve_2x2_system()
.map(|solution| solution.0 * 3 + solution.1)
})
.sum::<usize>()
.to_string()
}
}
impl Day13 {
fn parse(&self, input: &str) -> Vec<Machine> {
input
.split_terminator("\n\n")
.map(|s| s.parse::<Machine>().unwrap())
.collect()
}
}
#[derive(Debug)]
struct Machine {
a: Point,
b: Point,
prize_location: Point,
}
impl Machine {
fn solve_2x2_system(&self) -> Option<(usize, usize)> {
let a = self.a.x;
let b = self.b.x;
let c = self.a.y;
let d = self.b.y;
let e = self.prize_location.x;
let f = self.prize_location.y;
// Compute the determinant
let det = a * d - b * c;
// Check if determinant is zero (no unique solution)
if det == 0 {
return None; // System is singular
}
// Compute the numerators for x and y
let numerator_x = e * d - b * f;
let numerator_y = a * f - e * c;
// Check if solutions are divisible by determinant
if numerator_x % det != 0 || numerator_y % det != 0 {
return None; // No integer solution
}
// Compute the solutions
let x = numerator_x / det;
let y = numerator_y / det;
Some((x as usize, y as usize))
}
fn move_prize_by_10000000000000(&self) -> Self {
const DIFF: isize = 10000000000000;
let diff = Point::new(DIFF, DIFF);
Self {
a: self.a,
b: self.b,
prize_location: self.prize_location + diff,
}
}
}
impl FromStr for Machine {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (button_a_str, button_b_str, prize_str) = s.lines().collect_tuple().unwrap();
let prize_location = prize_str
.trim_start_matches("Prize: ")
.split_once(", ")
.map(|(x, y)| {
Point::new(
x.trim_start_matches("X=").parse().unwrap(),
y.trim_start_matches("Y=").parse().unwrap(),
)
})
.unwrap();
let button_fn = |source: &str| -> Point {
source
.trim_start_matches("Button A: ")
.trim_start_matches("Button B: ")
.split_once(", ")
.map(|(x, y)| {
Point::new(
x.trim_start_matches("X+").parse().unwrap(),
y.trim_start_matches("Y+").parse().unwrap(),
)
})
.unwrap()
};
Ok(Self {
a: button_fn(button_a_str),
b: button_fn(button_b_str),
prize_location,
})
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day13::Day13;
use crate::solutions::Solution;
const EXAMPLE: &str = r#"Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279"#;
#[test]
fn part_one_example_test() {
assert_eq!("480", Day13.part_one(EXAMPLE));
}
#[test]
fn part_two_example_test() {
assert_eq!("875318608908", Day13.part_two(EXAMPLE));
}
}