|
| 1 | +// Copyright (c) 2025 Lexi Robinson |
| 2 | +// |
| 3 | +// Licensed under the EUPL, Version 1.2 |
| 4 | +// |
| 5 | +// You may not use this work except in compliance with the Licence. |
| 6 | +// You should have received a copy of the Licence along with this work. If not, see: |
| 7 | +// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>. |
| 8 | +// See the Licence for the specific language governing permissions and limitations under the Licence. |
| 9 | + |
| 10 | +use std::fmt::Display; |
| 11 | + |
| 12 | +use crate::{CharGrid, CommonGrid, Coord2D, Grid, symbols}; |
| 13 | + |
| 14 | +enum GridState { |
| 15 | + Snow, |
| 16 | + Tree, |
| 17 | +} |
| 18 | + |
| 19 | +impl TryFrom<char> for GridState { |
| 20 | + type Error = crate::AoCError; |
| 21 | + |
| 22 | + fn try_from(value: char) -> Result<Self, Self::Error> { |
| 23 | + Ok(match value { |
| 24 | + '.' => Self::Snow, |
| 25 | + '#' => Self::Tree, |
| 26 | + _ => return Err(Self::Error::new_from_char(value)), |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Display for GridState { |
| 32 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 33 | + match self { |
| 34 | + Self::Snow => symbols::VOID, |
| 35 | + Self::Tree => symbols::TREE, |
| 36 | + } |
| 37 | + .fmt(f) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn toboggonate(grid: &Grid<GridState>, dir: Coord2D) -> usize { |
| 42 | + let mut pos = Coord2D { x: 0, y: 0 }; |
| 43 | + let mut trees = 0; |
| 44 | + loop { |
| 45 | + match grid.get(&pos) { |
| 46 | + Some(GridState::Tree) => trees += 1, |
| 47 | + Some(_) => (), |
| 48 | + None => { |
| 49 | + log::debug!("Fell of the end of the map at {pos}"); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + pos += dir; |
| 54 | + pos.x %= grid.width as i32; |
| 55 | + } |
| 56 | + |
| 57 | + trees |
| 58 | +} |
| 59 | + |
| 60 | +pub fn part_1(data: crate::DataIn) -> crate::AoCResult<String> { |
| 61 | + let grid: Grid<GridState> = Grid::new_from_chars(data)?; |
| 62 | + log::debug!("\n{grid:#}"); |
| 63 | + let ret = toboggonate(&grid, Coord2D { x: 3, y: 1 }); |
| 64 | + Ok(ret.to_string()) |
| 65 | +} |
| 66 | + |
| 67 | +pub fn part_2(data: crate::DataIn) -> crate::AoCResult<String> { |
| 68 | + let grid: Grid<GridState> = Grid::new_from_chars(data)?; |
| 69 | + log::debug!("\n{grid:#}"); |
| 70 | + let ret: usize = [ |
| 71 | + Coord2D { x: 1, y: 1 }, |
| 72 | + Coord2D { x: 3, y: 1 }, |
| 73 | + Coord2D { x: 5, y: 1 }, |
| 74 | + Coord2D { x: 7, y: 1 }, |
| 75 | + Coord2D { x: 1, y: 2 }, |
| 76 | + ] |
| 77 | + .into_iter() |
| 78 | + .map(|coord| toboggonate(&grid, coord)) |
| 79 | + .product(); |
| 80 | + |
| 81 | + Ok(ret.to_string()) |
| 82 | +} |
| 83 | + |
| 84 | +inventory::submit!(crate::AoCDay { |
| 85 | + year: "2020", |
| 86 | + day: "3", |
| 87 | + part_1: crate::AoCPart { |
| 88 | + main: part_1, |
| 89 | + example: part_1 |
| 90 | + }, |
| 91 | + part_2: Some(crate::AoCPart { |
| 92 | + main: part_2, |
| 93 | + example: part_2 |
| 94 | + }), |
| 95 | +}); |
0 commit comments