|
1 | 1 | use crate::solutions::Solution; |
| 2 | +use crate::utils::grid::{Grid, PrintableOnGrid}; |
| 3 | +use crate::utils::line::Line; |
| 4 | +use crate::utils::point::Point; |
| 5 | +use std::collections::{HashSet, VecDeque}; |
| 6 | + |
| 7 | +const START: char = 'S'; |
| 8 | +const SPLITTER: char = '^'; |
| 9 | +const BEAM: char = '|'; |
2 | 10 |
|
3 | 11 | pub struct Day07; |
4 | 12 |
|
5 | 13 | impl Solution for Day07 { |
6 | | - fn part_one(&self, _input: &str) -> String { |
7 | | - String::from("0") |
| 14 | + fn part_one(&self, input: &str) -> String { |
| 15 | + let grid: Grid<char> = Grid::from(input); |
| 16 | + let max_height = grid.columns_range().end(); |
| 17 | + let start = grid.get_first_position(&START).unwrap(); |
| 18 | + |
| 19 | + let splitters: HashSet<Point> = grid.get_all_positions(&SPLITTER).into_iter().collect(); |
| 20 | + |
| 21 | + let mut result_beams: Vec<Beam> = Vec::new(); |
| 22 | + let mut current_beams: VecDeque<Beam> = VecDeque::from(vec![start.into()]); |
| 23 | + |
| 24 | + while let Some(current_beam) = current_beams.pop_front() { |
| 25 | + let down = current_beam.down(); |
| 26 | + |
| 27 | + if splitters.contains(&down.current()) { |
| 28 | + result_beams.push(current_beam); |
| 29 | + |
| 30 | + for other in down.split() { |
| 31 | + if result_beams.iter().any(|beam| beam.is_on(&other)) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + current_beams.push_back(other); |
| 36 | + } |
| 37 | + |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if down.current().y > max_height + 1 { |
| 42 | + result_beams.push(current_beam); |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + current_beams.push_front(down); |
| 47 | + } |
| 48 | + |
| 49 | + let mut new = grid.clone(); |
| 50 | + new.print(&result_beams[..]); |
| 51 | + |
| 52 | + println!("{}", new); |
| 53 | + |
| 54 | + result_beams.len().to_string() |
8 | 55 | } |
9 | 56 |
|
10 | 57 | fn part_two(&self, _input: &str) -> String { |
11 | 58 | String::from("0") |
12 | 59 | } |
13 | 60 | } |
14 | 61 |
|
| 62 | +#[derive(Copy, Clone)] |
| 63 | +struct Beam { |
| 64 | + line: Line, |
| 65 | +} |
| 66 | + |
| 67 | +impl Beam { |
| 68 | + fn is_on(&self, other: &Self) -> bool { |
| 69 | + if other.line.start() != other.line.end() { |
| 70 | + panic!("We only support beam that just started"); |
| 71 | + } |
| 72 | + |
| 73 | + self.line.is_on(&other.line.start()) |
| 74 | + } |
| 75 | + |
| 76 | + fn down(&self) -> Self { |
| 77 | + Self { |
| 78 | + line: Line::new(self.line.start(), self.line.end().south()), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fn current(&self) -> Point { |
| 83 | + self.line.end() |
| 84 | + } |
| 85 | + |
| 86 | + fn split(&self) -> Vec<Beam> { |
| 87 | + vec![self.current().west().into(), self.current().east().into()] |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl From<Point> for Beam { |
| 92 | + fn from(value: Point) -> Self { |
| 93 | + Self { |
| 94 | + line: Line::new(value, value), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl PrintableOnGrid for Beam { |
| 100 | + type Cell = char; |
| 101 | + |
| 102 | + fn print_on_grid(&self, grid: &mut Grid<char>) { |
| 103 | + let mut moved = self.line.start(); |
| 104 | + |
| 105 | + loop { |
| 106 | + grid.modify(moved, BEAM); |
| 107 | + if moved == self.line.end() { |
| 108 | + break; |
| 109 | + } |
| 110 | + moved = moved.south(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
15 | 115 | #[cfg(test)] |
16 | 116 | mod tests { |
17 | 117 | use crate::solutions::year2025::day07::Day07; |
18 | 118 | use crate::solutions::Solution; |
19 | 119 |
|
20 | | - const EXAMPLE: &str = r#""#; |
| 120 | + const EXAMPLE: &str = r#".......S....... |
| 121 | +............... |
| 122 | +.......^....... |
| 123 | +............... |
| 124 | +......^.^...... |
| 125 | +............... |
| 126 | +.....^.^.^..... |
| 127 | +............... |
| 128 | +....^.^...^.... |
| 129 | +............... |
| 130 | +...^.^...^.^... |
| 131 | +............... |
| 132 | +..^...^.....^.. |
| 133 | +............... |
| 134 | +.^.^.^.^.^...^. |
| 135 | +..............."#; |
21 | 136 |
|
22 | 137 | #[test] |
23 | 138 | fn part_one_example_test() { |
24 | | - assert_eq!("0", Day07.part_one(EXAMPLE)); |
| 139 | + assert_eq!("21", Day07.part_one(EXAMPLE)); |
25 | 140 | } |
26 | 141 | } |
0 commit comments