diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2024-12-16 20:55:42 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2024-12-16 20:55:42 +0100 |
| commit | 00def0f3a8109ed65d7d1024d031d1349f3a0778 (patch) | |
| tree | a7281aa02fae36e6a626813922abf58775a6120d | |
| parent | ec8a232c57aca9fda5d9a2797eb618e5f072db25 (diff) | |
day13 solution 1
| -rw-r--r-- | src/bin/day13.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/bin/day13.rs b/src/bin/day13.rs new file mode 100644 index 0000000..965d7c0 --- /dev/null +++ b/src/bin/day13.rs @@ -0,0 +1,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); + } +} |
