Skip to content

Commit

Permalink
Year 2024, Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
kurisuke committed Dec 12, 2024
1 parent 50dd0be commit a9e809b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
82 changes: 82 additions & 0 deletions year2024/src/day10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::collections::{HashSet, VecDeque};

use common::day::Day;
use util::grid2d::{Coords, Grid2D};

pub struct Day10 {}

impl Day for Day10 {
fn star1(&self, input: &str) -> String {
let grid = Grid2D::new_by(input, |c| c.to_digit(10).unwrap() as u8).unwrap();
let score = grid
.filter(&[0])
.into_iter()
.map(|trailhead| trail_score(&grid, trailhead, false))
.sum::<usize>();
format!("{}", score)
}

fn star2(&self, input: &str) -> String {
let grid = Grid2D::new_by(input, |c| c.to_digit(10).unwrap() as u8).unwrap();
let rating = grid
.filter(&[0])
.into_iter()
.map(|trailhead| trail_score(&grid, trailhead, true))
.sum::<usize>();
format!("{}", rating)
}
}

fn trail_score(grid: &Grid2D<u8>, trailhead: Coords, all_paths: bool) -> usize {
let mut score = 0;
let mut frontier = VecDeque::new();
frontier.push_back(trailhead);
let mut visited = HashSet::new();
if !all_paths {
visited.insert(trailhead);
}

while let Some(pos) = frontier.pop_front() {
let pos_height = grid.at(&pos).unwrap();
for neighbor_pos in grid.neighbors_cardinal_coords(&pos) {
if let Some(neighbor_height) = grid.at(&neighbor_pos) {
if *neighbor_height == pos_height + 1 && (all_paths || visited.insert(neighbor_pos))
{
if *neighbor_height == 9 {
score += 1;
} else {
frontier.push_back(neighbor_pos);
}
}
}
}
}

score
}

#[cfg(test)]
mod tests {
use super::*;

const INPUT: &str = r#"89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732"#;

#[test]
fn star1() {
let d = Day10 {};
assert_eq!(d.star1(INPUT), "36");
}

#[test]
fn star2() {
let d = Day10 {};
assert_eq!(d.star2(INPUT), "81");
}
}
2 changes: 2 additions & 0 deletions year2024/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod day06;
mod day07;
mod day08;
mod day09;
mod day10;

pub struct Year2024 {}

Expand All @@ -25,6 +26,7 @@ impl Year for Year2024 {
7 => Some(Box::new(day07::Day07 {})),
8 => Some(Box::new(day08::Day08 {})),
9 => Some(Box::new(day09::Day09 {})),
10 => Some(Box::new(day10::Day10 {})),
_ => None,
}
}
Expand Down

0 comments on commit a9e809b

Please sign in to comment.