Skip to content

Commit

Permalink
2024_10
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaikia committed Dec 10, 2024
1 parent d5cc2f8 commit 2bf9616
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/bin/2024_09/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Debug;

use aoc::{common, range::Range};

#[derive(Clone)]
Expand All @@ -13,17 +11,19 @@ struct Space {
r: Range<usize>,
}

impl Debug for Space {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl Space {
fn print(spaces: &[Space]) {
let mut s = String::new();
for _ in self.r.a..self.r.b {
let t = match &self.t {
SpaceType::Free => ".",
SpaceType::Occupied(id) => &id.to_string(),
};
s.push_str(t);
for sp in spaces {
for _ in sp.r.a..sp.r.b {
let t = match &sp.t {
SpaceType::Free => ".",
SpaceType::Occupied(id) => &id.to_string(),
};
s.push_str(t);
}
}
write!(f, "{}", s)
println!("{s}");
}
}

Expand Down Expand Up @@ -86,6 +86,8 @@ fn solve<const PART: i32>(input: &str) -> usize {
}
}

Space::print(&spaces);

let mut ans = 0;
for s in spaces {
if let SpaceType::Occupied(id) = s.t {
Expand Down
51 changes: 51 additions & 0 deletions src/bin/2024_10/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use aoc::{common, grid::Grid};

fn solve<const PART: i32>(input: &str) -> usize {
let mut ans = 0;
let map = Grid::from_str(input, |c| c.to_digit(10).unwrap());
let trailheads = map.positions(0);
for pos in trailheads {
let mut q = Vec::new();
q.push((pos, 0));
let mut trails = Vec::new();

while let Some(tp) = q.pop() {
if tp.1 == 9 {
trails.push(tp.0);
continue;
}
let nns = map.adjacent_4(&tp.0);
for nn in nns.iter() {
if map.get(nn) == tp.1 + 1 {
q.push((*nn, tp.1 + 1));
}
}
}
if PART == 1 {
trails.sort();
trails.dedup();
}
ans += trails.len();
}
ans
}

fn main() {
let input = common::get_input();
//println!("{input:?}");
common::timed(&input, solve::<1>, true);
common::timed(&input, solve::<2>, false);
}

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

#[test]
fn test_samples() {
let sample_input =
"89010123\n78121874\n87430965\n96549874\n45678903\n32019012\n01329801\n10456732";
assert_eq!(solve::<1>(sample_input), 36);
assert_eq!(solve::<2>(sample_input), 81);
}
}

0 comments on commit 2bf9616

Please sign in to comment.