|
| 1 | +use itertools::Itertools; |
| 2 | + |
| 3 | +advent_of_code::solution!(14); |
| 4 | + |
| 5 | +#[cfg(not(test))] |
| 6 | +const WIDTH: u8 = 101; |
| 7 | +#[cfg(not(test))] |
| 8 | +const HEIGHT: u8 = 103; |
| 9 | + |
| 10 | +#[cfg(test)] |
| 11 | +const WIDTH: u8 = 11; |
| 12 | +#[cfg(test)] |
| 13 | +const HEIGHT: u8 = 7; |
| 14 | + |
| 15 | +#[derive(Debug)] |
| 16 | +struct Robot { |
| 17 | + position: (isize, isize), |
| 18 | + velocity: (isize, isize), |
| 19 | +} |
| 20 | + |
| 21 | +impl Robot { |
| 22 | + fn parse(input: &str) -> Robot { |
| 23 | + let pos_x = &input[input.find('=').unwrap() + 1..input.find(',').unwrap()]; |
| 24 | + let pos_y = &input[input.find(',').unwrap() + 1..input.find(' ').unwrap()]; |
| 25 | + |
| 26 | + let vel_x = &input[input.rfind('=').unwrap() + 1..input.rfind(',').unwrap()]; |
| 27 | + let vel_y = &input[input.rfind(',').unwrap() + 1..]; |
| 28 | + |
| 29 | + Robot { |
| 30 | + position: (pos_x.parse().unwrap(), pos_y.parse().unwrap()), |
| 31 | + velocity: (vel_x.parse().unwrap(), vel_y.parse().unwrap()), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + fn simulate(&mut self, seconds: isize) { |
| 36 | + let (offset_x, offset_y) = (self.velocity.0 * seconds, self.velocity.1 * seconds); |
| 37 | + let new_position = (self.position.0 + offset_x, self.position.1 + offset_y); |
| 38 | + self.position = (new_position.0.rem_euclid(WIDTH as isize), new_position.1.rem_euclid(HEIGHT as isize)); |
| 39 | + } |
| 40 | + |
| 41 | + fn get_quadrant(&self) -> Option<u8> { |
| 42 | + let (center_column, center_row) = (WIDTH as isize / 2, HEIGHT as isize / 2); |
| 43 | + if self.position.0 < center_column && self.position.1 < center_row { |
| 44 | + Some(0) |
| 45 | + } else if self.position.0 > center_column && self.position.1 < center_row { |
| 46 | + Some(1) |
| 47 | + } else if self.position.0 < center_column && self.position.1 > center_row { |
| 48 | + Some(2) |
| 49 | + } else if self.position.0 > center_column && self.position.1 > center_row { |
| 50 | + Some(3) |
| 51 | + } else { |
| 52 | + None |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +pub fn part_one(input: &str) -> Option<u32> { |
| 58 | + let mut quadrants = [0; 4]; |
| 59 | + input.lines() |
| 60 | + .map(Robot::parse) |
| 61 | + .map(|mut robot| { |
| 62 | + robot.simulate(100); |
| 63 | + robot |
| 64 | + }) |
| 65 | + .filter_map(|robot| robot.get_quadrant()) |
| 66 | + .for_each(|quadrant| quadrants[quadrant as usize] += 1); |
| 67 | + Some(quadrants.iter().product()) |
| 68 | +} |
| 69 | + |
| 70 | +pub fn part_two(input: &str) -> Option<u32> { |
| 71 | + let (mut lowest_safety_factor, mut seconds_passed) = (u32::MAX, 0); |
| 72 | + let mut robots = input.lines().map(Robot::parse).collect_vec(); |
| 73 | + for second in 1..=WIDTH as u16 * HEIGHT as u16 { |
| 74 | + let mut quadrants = [0; 4]; |
| 75 | + robots.iter_mut() |
| 76 | + .map(|robot| { |
| 77 | + robot.simulate(1); |
| 78 | + robot |
| 79 | + }) |
| 80 | + .filter_map(|robot| robot.get_quadrant()) |
| 81 | + .for_each(|quadrant| quadrants[quadrant as usize] += 1); |
| 82 | + let safety_factor = quadrants.iter().product(); |
| 83 | + if safety_factor < lowest_safety_factor { |
| 84 | + lowest_safety_factor = safety_factor; |
| 85 | + seconds_passed = second; |
| 86 | + } |
| 87 | + } |
| 88 | + Some(seconds_passed as u32) |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_part_one() { |
| 97 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 98 | + assert_eq!(result, Some(12)); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_part_two() { |
| 103 | + // Cannot test part two |
| 104 | + } |
| 105 | +} |
0 commit comments