Skip to content

Commit

Permalink
day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed Dec 11, 2024
1 parent 46ba00f commit fcb423d
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 7](./src/bin/07.rs) | `487.3µs` | `862.0µs` |
| [Day 8](./src/bin/08.rs) | `36.5µs` | `116.8µs` |
| [Day 9](./src/bin/09.rs) | `80.1µs` | `258.4ms` |
| [Day 10](./src/bin/10.rs) | `464.5µs` | `28.4µs` |

**Total: 293.85ms**
**Total: 294.35ms**
<!--- benchmarking table --->
8 changes: 8 additions & 0 deletions data/examples/10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
176 changes: 176 additions & 0 deletions src/bin/10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use std::{collections::HashSet, ops::Add};

use itertools::Itertools;

advent_of_code::solution!(10);

#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
struct Position(usize, usize);

pub fn part_one(input: &str) -> Option<usize> {
let mut trailheads = vec![];

let map = input
.lines()
.enumerate()
.map(|(ydx, line)| {
line.chars()
.enumerate()
.map(|(xdx, ch)| {
let num = ch.to_digit(10).unwrap_or(100);
if num == 0 {
trailheads.push((xdx, ydx));
}
num
})
.collect_vec()
})
.collect_vec();

let mut res = 0;
for th in trailheads {
let tmp = dfs_step(&map, Position(th.0, th.1), 0);
res += tmp.len();
}

Some(res)
}

fn dfs_step(map: &[Vec<u32>], current_pos: Position, current_val: u32) -> HashSet<Position> {
if current_val == 9 {
let mut set = HashSet::new();
set.insert(current_pos);
return set;
}

let mut result = HashSet::new();
// Up
if current_pos.1 > 0 && map[current_pos.1 - 1][current_pos.0] == current_val + 1 {
result.extend(dfs_step(
map,
Position(current_pos.0, current_pos.1 - 1),
current_val + 1,
));
};

// Righht
if current_pos.0 < map[0].len() - 1 && map[current_pos.1][current_pos.0 + 1] == current_val + 1
{
result.extend(dfs_step(
map,
Position(current_pos.0 + 1, current_pos.1),
current_val + 1,
));
};

// Down
if current_pos.1 < map.len() - 1 && map[current_pos.1 + 1][current_pos.0] == current_val + 1 {
result.extend(dfs_step(
map,
Position(current_pos.0, current_pos.1 + 1),
current_val + 1,
));
};

// Left
if current_pos.0 > 0 && map[current_pos.1][current_pos.0 - 1] == current_val + 1 {
result.extend(dfs_step(
map,
Position(current_pos.0 - 1, current_pos.1),
current_val + 1,
));
};

result
}

pub fn part_two(input: &str) -> Option<u32> {
let mut trailheads = vec![];

let map = input
.lines()
.enumerate()
.map(|(ydx, line)| {
line.chars()
.enumerate()
.map(|(xdx, ch)| {
let num = ch.to_digit(10).unwrap_or(100);
if num == 0 {
trailheads.push((xdx, ydx));
}
num
})
.collect_vec()
})
.collect_vec();

let mut res = 0;
for th in trailheads {
res += dfs_step_2(&map, Position(th.0, th.1), 0);
}

Some(res)
}

fn dfs_step_2(map: &[Vec<u32>], current_pos: Position, current_val: u32) -> u32 {
if current_val == 9 {
return 1;
}

let mut result: u32 = 0;
// Up
if current_pos.1 > 0 && map[current_pos.1 - 1][current_pos.0] == current_val + 1 {
result = result.add(dfs_step_2(
map,
Position(current_pos.0, current_pos.1 - 1),
current_val + 1,
));
};

// Righht
if current_pos.0 < map[0].len() - 1 && map[current_pos.1][current_pos.0 + 1] == current_val + 1
{
result = result.add(dfs_step_2(
map,
Position(current_pos.0 + 1, current_pos.1),
current_val + 1,
));
};

// Down
if current_pos.1 < map.len() - 1 && map[current_pos.1 + 1][current_pos.0] == current_val + 1 {
result = result.add(dfs_step_2(
map,
Position(current_pos.0, current_pos.1 + 1),
current_val + 1,
));
};

// Left
if current_pos.0 > 0 && map[current_pos.1][current_pos.0 - 1] == current_val + 1 {
result = result.add(dfs_step_2(
map,
Position(current_pos.0 - 1, current_pos.1),
current_val + 1,
));
};

result
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}

0 comments on commit fcb423d

Please sign in to comment.