Skip to content

Commit

Permalink
add day10
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 10, 2024
1 parent f2450bb commit b92af35
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 2024/day10/.gitignore
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

14 changes: 14 additions & 0 deletions 2024/day10/Cargo.toml
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 }
95 changes: 95 additions & 0 deletions 2024/day10/src/main.rs
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);
}
}

0 comments on commit b92af35

Please sign in to comment.