|
1 | 1 | use crate::solutions::Solution; |
| 2 | +use crate::utils::grid::Grid; |
| 3 | +use crate::utils::point::Point; |
| 4 | +use crate::utils::surface_range::SurfaceRange; |
| 5 | +use std::fmt::Debug; |
2 | 6 |
|
3 | 7 | pub struct Day06; |
4 | 8 |
|
5 | 9 | impl Solution for Day06 { |
6 | | - fn part_one(&self, _input: &str) -> String { |
7 | | - String::from("0") |
| 10 | + fn part_one(&self, input: &str) -> String { |
| 11 | + let mut grid = Grid::filled( |
| 12 | + SurfaceRange::from((Point::new(0, 0), Point::new(999, 999))), |
| 13 | + false, |
| 14 | + ); |
| 15 | + let instructions = self.parse(input); |
| 16 | + |
| 17 | + for instruction in instructions { |
| 18 | + instruction.apply(&mut grid); |
| 19 | + } |
| 20 | + |
| 21 | + grid.get_all_positions(&true).len().to_string() |
8 | 22 | } |
9 | 23 |
|
10 | 24 | fn part_two(&self, _input: &str) -> String { |
11 | 25 | String::from("0") |
12 | 26 | } |
13 | 27 | } |
14 | 28 |
|
| 29 | +impl Day06 { |
| 30 | + fn parse(&self, input: &str) -> Vec<Box<dyn Instruction>> { |
| 31 | + input |
| 32 | + .lines() |
| 33 | + .map(|line| -> Box<dyn Instruction> { |
| 34 | + if line.starts_with("turn on ") { |
| 35 | + let trimmed = line.trim_start_matches("turn on "); |
| 36 | + |
| 37 | + Box::new(TurnOn::from(self.parse_points(trimmed))) |
| 38 | + } else if line.starts_with("turn off ") { |
| 39 | + let trimmed = line.trim_start_matches("turn off "); |
| 40 | + |
| 41 | + Box::new(TurnOff::from(self.parse_points(trimmed))) |
| 42 | + } else if line.starts_with("toggle ") { |
| 43 | + let trimmed = line.trim_start_matches("toggle "); |
| 44 | + |
| 45 | + Box::new(Toggle::from(self.parse_points(trimmed))) |
| 46 | + } else { |
| 47 | + unreachable!() |
| 48 | + } |
| 49 | + }) |
| 50 | + .collect() |
| 51 | + } |
| 52 | + |
| 53 | + fn parse_points(&self, points_str: &str) -> (Point, Point) { |
| 54 | + let p = points_str.split_once(" through ").unwrap(); |
| 55 | + |
| 56 | + (p.0.parse().unwrap(), p.1.parse().unwrap()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +trait Instruction: Debug { |
| 61 | + fn apply(&self, grid: &mut Grid<bool>); |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug)] |
| 65 | +struct TurnOn { |
| 66 | + surface_range: SurfaceRange, |
| 67 | +} |
| 68 | + |
| 69 | +impl From<(Point, Point)> for TurnOn { |
| 70 | + fn from(value: (Point, Point)) -> Self { |
| 71 | + Self { |
| 72 | + surface_range: SurfaceRange::from(value), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl Instruction for TurnOn { |
| 78 | + fn apply(&self, grid: &mut Grid<bool>) { |
| 79 | + grid.modify_many(self.surface_range.points(), true) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Debug)] |
| 84 | +struct TurnOff { |
| 85 | + surface_range: SurfaceRange, |
| 86 | +} |
| 87 | + |
| 88 | +impl From<(Point, Point)> for TurnOff { |
| 89 | + fn from(value: (Point, Point)) -> Self { |
| 90 | + Self { |
| 91 | + surface_range: SurfaceRange::from(value), |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl Instruction for TurnOff { |
| 97 | + fn apply(&self, grid: &mut Grid<bool>) { |
| 98 | + grid.modify_many(self.surface_range.points(), false) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[derive(Debug)] |
| 103 | +struct Toggle { |
| 104 | + surface_range: SurfaceRange, |
| 105 | +} |
| 106 | + |
| 107 | +impl From<(Point, Point)> for Toggle { |
| 108 | + fn from(value: (Point, Point)) -> Self { |
| 109 | + Self { |
| 110 | + surface_range: SurfaceRange::from(value), |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl Instruction for Toggle { |
| 116 | + fn apply(&self, grid: &mut Grid<bool>) { |
| 117 | + grid.modify_many_with(self.surface_range.points(), |b| *b = !*b) |
| 118 | + } |
| 119 | +} |
| 120 | + |
15 | 121 | #[cfg(test)] |
16 | 122 | mod tests { |
17 | 123 | use super::*; |
18 | 124 |
|
19 | 125 | #[test] |
20 | 126 | fn part_one_example_test() { |
21 | | - assert_eq!("0", Day06.part_one("")); |
| 127 | + assert_eq!("1000000", Day06.part_one("turn on 0,0 through 999,999")); |
| 128 | + assert_eq!("1000", Day06.part_one("toggle 0,0 through 999,0")); |
22 | 129 | } |
23 | 130 | } |
0 commit comments