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
|
use regex::Regex;
static DAY: u8 = 13;
fn main() {
let input = advent::read_file(DAY);
println!("{DAY}a: {}", win_tokens(input.trim_end()));
println!("{DAY}b: {}", 0);
}
struct Machine {
button_a: (u32, u32),
button_b: (u32, u32),
prize: (u32, u32),
}
impl Machine {
fn new(input: &str) -> Machine {
let re_a = Regex::new(r"Button A: X\+(\d+), Y\+(\d+)").unwrap();
let re_b = Regex::new(r"Button B: X\+(\d+), Y\+(\d+)").unwrap();
let re_p = Regex::new(r"Prize: X=(\d+), Y=(\d+)").unwrap();
let lines = input.split("\n").collect::<Vec<_>>();
let button_a = lines[0];
let button_b = lines[1];
let prizes = lines[2];
let captures_a = re_a.captures(button_a).unwrap();
let captures_b = re_b.captures(button_b).unwrap();
let captures_p = re_p.captures(prizes).unwrap();
let a_x = captures_a.get(1).unwrap().as_str().parse::<u32>().unwrap();
let a_y = captures_a.get(2).unwrap().as_str().parse::<u32>().unwrap();
let b_x = captures_b.get(1).unwrap().as_str().parse::<u32>().unwrap();
let b_y = captures_b.get(2).unwrap().as_str().parse::<u32>().unwrap();
let p_x = captures_p.get(1).unwrap().as_str().parse::<u32>().unwrap();
let p_y = captures_p.get(2).unwrap().as_str().parse::<u32>().unwrap();
Machine {
button_a: (a_x, a_y),
button_b: (b_x, b_y),
prize: (p_x, p_y),
}
}
fn lowest_prize(&self) -> Option<u32> {
let mut best_prize = None;
for a in 0 ..= 100 {
for b in 0 ..= 100 {
if (a * self.button_a.0) + (b * self.button_b.0) != self.prize.0 {
continue;
}
if (a * self.button_a.1) + (b * self.button_b.1) != self.prize.1 {
continue;
}
let prize = 3 * a + b;
best_prize = Some(best_prize.unwrap_or(u32::MAX).min(prize));
}
}
best_prize
}
}
fn win_tokens(input: &str) -> u32 {
input.split("\n\n")
.map(Machine::new)
.map(|machine| machine.lowest_prize().unwrap_or(0))
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input =
"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";
assert_eq!(win_tokens(&input), 480);
}
}
|