-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
input.txt | ||
flamegraph.svg | ||
perf.data* | ||
### Rust | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "day10" | ||
authors = ["mirsella <mirsella@protonmail.com>"] | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
regex = { workspace = true } | ||
itertools = { workspace = true } | ||
pathfinding = { workspace = true } | ||
rayon = { workspace = true } | ||
indexmap = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use pathfinding::matrix::Matrix; | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
fn part1(input: &str) -> usize { | ||
let m = Matrix::from_rows( | ||
input | ||
.lines() | ||
.map(|l| l.chars().map(|c| c.to_digit(10).unwrap() as u8)), | ||
) | ||
.unwrap(); | ||
fn parkour(m: &Matrix<u8>, trailends: &mut HashSet<(usize, usize)>, pos: (usize, usize)) { | ||
if m[pos] == 9 { | ||
trailends.insert(pos); | ||
return; | ||
} | ||
for neighbor in m.neighbours(pos, false) { | ||
if m[neighbor] == m[pos] + 1 { | ||
parkour(m, trailends, neighbor); | ||
} | ||
} | ||
} | ||
let mut trailheads = HashMap::new(); | ||
let mut tmp_trailends = HashSet::new(); | ||
for (pos, &char) in m.items() { | ||
if char != 0 { | ||
continue; | ||
} | ||
parkour(&m, &mut tmp_trailends, pos); | ||
trailheads.insert(pos, tmp_trailends.len()); | ||
tmp_trailends.clear(); | ||
} | ||
trailheads.values().sum() | ||
} | ||
fn part2(input: &str) -> usize { | ||
let m = Matrix::from_rows( | ||
input | ||
.lines() | ||
.map(|l| l.chars().map(|c| c.to_digit(10).unwrap() as u8)), | ||
) | ||
.unwrap(); | ||
fn parkour( | ||
m: &Matrix<u8>, | ||
trailends: &mut HashSet<Vec<(usize, usize)>>, | ||
mut current: Vec<(usize, usize)>, | ||
pos: (usize, usize), | ||
) { | ||
current.push(pos); | ||
if m[pos] == 9 { | ||
trailends.insert(current); | ||
return; | ||
} | ||
for neighbor in m.neighbours(pos, false) { | ||
if m[neighbor] == m[pos] + 1 { | ||
parkour(m, trailends, current.clone(), neighbor); | ||
} | ||
} | ||
} | ||
let mut trailheads = HashMap::new(); | ||
let mut tmp_trailends = HashSet::new(); | ||
for (pos, &char) in m.items() { | ||
if char != 0 { | ||
continue; | ||
} | ||
parkour(&m, &mut tmp_trailends, Vec::new(), pos); | ||
trailheads.insert(pos, tmp_trailends.len()); | ||
tmp_trailends.clear(); | ||
} | ||
trailheads.values().sum() | ||
} | ||
|
||
fn main() { | ||
let input = include_str!("../input.txt"); | ||
println!("Part 1: {}", part1(input)); | ||
println!("Part 2: {}", part2(input)); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
const INPUT: &str = "89010123 | ||
78121874 | ||
87430965 | ||
96549874 | ||
45678903 | ||
32019012 | ||
01329801 | ||
10456732"; | ||
#[test] | ||
fn part1() { | ||
assert_eq!(super::part1(INPUT), 36); | ||
} | ||
#[test] | ||
fn part2() { | ||
assert_eq!(super::part2(INPUT), 81); | ||
} | ||
} |