|
| 1 | +#![allow(non_snake_case)] |
| 2 | + |
| 3 | +use aoc::geometry::{Translate, XY}; |
| 4 | +use aoc::Puzzle; |
| 5 | + |
| 6 | +const W: usize = 101; |
| 7 | +const H: usize = 103; |
| 8 | +type InputType = Vec<(XY, XY)>; |
| 9 | + |
| 10 | +struct AoC2024_14; |
| 11 | + |
| 12 | +impl AoC2024_14 { |
| 13 | + fn do_move( |
| 14 | + &self, |
| 15 | + robots: &InputType, |
| 16 | + w: usize, |
| 17 | + h: usize, |
| 18 | + rounds: usize, |
| 19 | + ) -> usize { |
| 20 | + let moved_robots: Vec<XY> = robots |
| 21 | + .iter() |
| 22 | + .map(|(pos, vec)| pos.translate(vec, rounds as i32)) |
| 23 | + .collect(); |
| 24 | + let mut q: [usize; 4] = [0, 0, 0, 0]; |
| 25 | + let (mid_x, mid_y) = (w / 2, h / 2); |
| 26 | + for pos in moved_robots { |
| 27 | + let (px, py) = ( |
| 28 | + pos.x().rem_euclid(w as i32) as usize, |
| 29 | + pos.y().rem_euclid(h as i32) as usize, |
| 30 | + ); |
| 31 | + if px < mid_x { |
| 32 | + if py < mid_y { |
| 33 | + q[0] += 1; |
| 34 | + } else if mid_y < py && py < h { |
| 35 | + q[1] += 1; |
| 36 | + } |
| 37 | + } else if mid_x < px && px < w { |
| 38 | + if py < mid_y { |
| 39 | + q[2] += 1; |
| 40 | + } else if mid_y < py && py < h { |
| 41 | + q[3] += 1; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + q.iter().product() |
| 46 | + } |
| 47 | + |
| 48 | + fn solve_1(&self, input: &InputType, w: usize, h: usize) -> usize { |
| 49 | + self.do_move(input, w, h, 100) |
| 50 | + } |
| 51 | + |
| 52 | + fn sample_part_1(&self, input: &InputType) -> usize { |
| 53 | + self.solve_1(input, 11, 7) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl aoc::Puzzle for AoC2024_14 { |
| 58 | + type Input = InputType; |
| 59 | + type Output1 = usize; |
| 60 | + type Output2 = usize; |
| 61 | + |
| 62 | + aoc::puzzle_year_day!(2024, 14); |
| 63 | + |
| 64 | + fn parse_input(&self, lines: Vec<String>) -> Self::Input { |
| 65 | + lines |
| 66 | + .iter() |
| 67 | + .map(|line| { |
| 68 | + let (p, v) = line.split_once(" ").unwrap(); |
| 69 | + let (px, py) = |
| 70 | + p.split_once("=").unwrap().1.split_once(",").unwrap(); |
| 71 | + let (vx, vy) = |
| 72 | + v.split_once("=").unwrap().1.split_once(",").unwrap(); |
| 73 | + ( |
| 74 | + XY::of( |
| 75 | + px.parse::<i32>().unwrap(), |
| 76 | + py.parse::<i32>().unwrap(), |
| 77 | + ), |
| 78 | + XY::of( |
| 79 | + vx.parse::<i32>().unwrap(), |
| 80 | + vy.parse::<i32>().unwrap(), |
| 81 | + ), |
| 82 | + ) |
| 83 | + }) |
| 84 | + .collect() |
| 85 | + } |
| 86 | + |
| 87 | + fn part_1(&self, robots: &Self::Input) -> Self::Output1 { |
| 88 | + self.solve_1(robots, W, H) |
| 89 | + } |
| 90 | + |
| 91 | + fn part_2(&self, robots: &Self::Input) -> Self::Output2 { |
| 92 | + let mut ans = 0; |
| 93 | + let mut best = usize::MAX; |
| 94 | + for round in 1..=W * H { |
| 95 | + let safety_factor = self.do_move(robots, W, H, round); |
| 96 | + if safety_factor < best { |
| 97 | + best = safety_factor; |
| 98 | + ans = round; |
| 99 | + } |
| 100 | + } |
| 101 | + ans |
| 102 | + } |
| 103 | + |
| 104 | + fn samples(&self) { |
| 105 | + aoc::puzzle_samples! { |
| 106 | + self, sample_part_1, TEST, 12 |
| 107 | + }; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +fn main() { |
| 112 | + AoC2024_14 {}.run(std::env::args()); |
| 113 | +} |
| 114 | + |
| 115 | +const TEST: &str = "\ |
| 116 | +p=0,4 v=3,-3 |
| 117 | +p=6,3 v=-1,-3 |
| 118 | +p=10,3 v=-1,2 |
| 119 | +p=2,0 v=2,-1 |
| 120 | +p=0,0 v=1,3 |
| 121 | +p=3,0 v=-2,-2 |
| 122 | +p=7,6 v=-1,-3 |
| 123 | +p=3,0 v=-1,-2 |
| 124 | +p=9,3 v=2,3 |
| 125 | +p=7,3 v=-1,2 |
| 126 | +p=2,4 v=2,-3 |
| 127 | +p=9,5 v=-3,-3 |
| 128 | +"; |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +mod tests { |
| 132 | + use super::*; |
| 133 | + |
| 134 | + #[test] |
| 135 | + pub fn samples() { |
| 136 | + AoC2024_14 {}.samples(); |
| 137 | + } |
| 138 | +} |
0 commit comments