Compare commits
No commits in common. "e39af55e7320f95d9ad33848ed43114126259174" and "30bd722f3eec46f72262f27b31321c149cda3ed9" have entirely different histories.
e39af55e73
...
30bd722f3e
|
@ -2,4 +2,3 @@
|
||||||
*.dump
|
*.dump
|
||||||
*.swp
|
*.swp
|
||||||
target
|
target
|
||||||
.DS_Store
|
|
||||||
|
|
|
@ -62,7 +62,6 @@ dependencies = [
|
||||||
"aoc-runner",
|
"aoc-runner",
|
||||||
"aoc-runner-derive",
|
"aoc-runner-derive",
|
||||||
"num",
|
"num",
|
||||||
"pathfinding",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -71,55 +70,6 @@ version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "deprecate-until"
|
|
||||||
version = "0.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7a3767f826efbbe5a5ae093920b58b43b01734202be697e1354914e862e8e704"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"semver",
|
|
||||||
"syn 2.0.39",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "equivalent"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "fixedbitset"
|
|
||||||
version = "0.4.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hashbrown"
|
|
||||||
version = "0.14.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "indexmap"
|
|
||||||
version = "2.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
|
|
||||||
dependencies = [
|
|
||||||
"equivalent",
|
|
||||||
"hashbrown",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "integer-sqrt"
|
|
||||||
version = "0.1.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770"
|
|
||||||
dependencies = [
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.9"
|
version = "1.0.9"
|
||||||
|
@ -214,21 +164,6 @@ dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pathfinding"
|
|
||||||
version = "4.8.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f6f4a3f5089b981000cb50ec24320faf7a19649a45e8730e4adf49f78f066528"
|
|
||||||
dependencies = [
|
|
||||||
"deprecate-until",
|
|
||||||
"fixedbitset",
|
|
||||||
"indexmap",
|
|
||||||
"integer-sqrt",
|
|
||||||
"num-traits",
|
|
||||||
"rustc-hash",
|
|
||||||
"thiserror",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.70"
|
version = "1.0.70"
|
||||||
|
@ -276,24 +211,12 @@ version = "0.8.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustc-hash"
|
|
||||||
version = "1.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.15"
|
version = "1.0.15"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "semver"
|
|
||||||
version = "1.0.20"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.193"
|
version = "1.0.193"
|
||||||
|
@ -349,18 +272,18 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.52"
|
version = "1.0.50"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d"
|
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror-impl"
|
name = "thiserror-impl"
|
||||||
version = "1.0.52"
|
version = "1.0.50"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3"
|
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|
|
@ -12,7 +12,6 @@ aoc-runner = "0.3.0"
|
||||||
aoc-runner-derive = "0.3.0"
|
aoc-runner-derive = "0.3.0"
|
||||||
aoc-parse = "0.2.17"
|
aoc-parse = "0.2.17"
|
||||||
num = "0.4"
|
num = "0.4"
|
||||||
pathfinding = "4.8.0"
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
incremental = true
|
incremental = true
|
||||||
|
|
|
@ -1,135 +0,0 @@
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
|
||||||
use pathfinding::matrix::{directions, Matrix};
|
|
||||||
use pathfinding::prelude::astar;
|
|
||||||
|
|
||||||
#[aoc_generator(day17)]
|
|
||||||
fn parse(input: &str) -> Matrix<usize> {
|
|
||||||
input
|
|
||||||
.lines()
|
|
||||||
.filter_map(|line| {
|
|
||||||
if !line.is_empty() {
|
|
||||||
Some(
|
|
||||||
line.chars()
|
|
||||||
.map(|c| c.to_digit(10).expect("bad digit") as usize),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<Matrix<usize>>()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Eq, Hash, PartialEq, Ord, PartialOrd)]
|
|
||||||
struct Path {
|
|
||||||
position: (usize, usize),
|
|
||||||
dir: (isize, isize),
|
|
||||||
steps: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn distance(s: &(usize, usize), d: &(usize, usize)) -> usize {
|
|
||||||
s.0.abs_diff(d.0) + s.1.abs_diff(d.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_path<const MIN: usize, const MAX: usize>(map: &Matrix<usize>) -> usize {
|
|
||||||
let start = Path {
|
|
||||||
position: (0, 0),
|
|
||||||
dir: (0, 0),
|
|
||||||
steps: 0,
|
|
||||||
};
|
|
||||||
let dest = (map.rows - 1, map.columns - 1);
|
|
||||||
|
|
||||||
let path = astar(
|
|
||||||
&start,
|
|
||||||
|cur| match cur.steps >= MIN || (cur.dir.0 == 0 && cur.dir.1 == 0) {
|
|
||||||
true => find_successors::<MAX>(map, cur),
|
|
||||||
false => continue_in_direction::<MIN>(map, cur),
|
|
||||||
},
|
|
||||||
|cur| distance(&dest, &cur.position),
|
|
||||||
|cur| cur.position == dest && cur.steps >= MIN,
|
|
||||||
)
|
|
||||||
.expect("path is missing");
|
|
||||||
path.1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_successors<const MAX: usize>(map: &Matrix<usize>, p: &Path) -> Vec<(Path, usize)> {
|
|
||||||
[directions::N, directions::E, directions::S, directions::W]
|
|
||||||
.iter()
|
|
||||||
.flat_map(|dir| {
|
|
||||||
map.move_in_direction(p.position, *dir)
|
|
||||||
.map(|cur| (cur, *dir, *map.get(cur).unwrap()))
|
|
||||||
})
|
|
||||||
.filter(|(cur, dir, _weight)| {
|
|
||||||
let backwards = p.dir.0 == -dir.0 && p.dir.1 == -dir.1;
|
|
||||||
!backwards && *cur != (0, 0)
|
|
||||||
})
|
|
||||||
.flat_map(|(cur, dir, weight)| {
|
|
||||||
let steps = match p.dir == dir {
|
|
||||||
true => p.steps + 1,
|
|
||||||
false => 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
match steps <= MAX {
|
|
||||||
true => {
|
|
||||||
let next = Path {
|
|
||||||
position: cur,
|
|
||||||
dir,
|
|
||||||
steps,
|
|
||||||
};
|
|
||||||
Some((next, weight))
|
|
||||||
}
|
|
||||||
false => None,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn continue_in_direction<const MIN: usize>(map: &Matrix<usize>, p: &Path) -> Vec<(Path, usize)> {
|
|
||||||
match map.move_in_direction(p.position, p.dir) {
|
|
||||||
Some(point) => {
|
|
||||||
let heat = *map.get(point).unwrap();
|
|
||||||
let new_path = Path {
|
|
||||||
position: point,
|
|
||||||
dir: p.dir,
|
|
||||||
steps: p.steps + 1,
|
|
||||||
};
|
|
||||||
vec![(new_path, heat)]
|
|
||||||
}
|
|
||||||
None => Vec::with_capacity(0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[aoc(day17, part1)]
|
|
||||||
fn part1(input: &Matrix<usize>) -> usize {
|
|
||||||
find_path::<1, 3>(&input)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[aoc(day17, part2)]
|
|
||||||
fn part2(input: &Matrix<usize>) -> usize {
|
|
||||||
find_path::<4, 10>(&input)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
const SAMPLE_DATA: &'static str = r#"2413432311323
|
|
||||||
3215453535623
|
|
||||||
3255245654254
|
|
||||||
3446585845452
|
|
||||||
4546657867536
|
|
||||||
1438598798454
|
|
||||||
4457876987766
|
|
||||||
3637877979653
|
|
||||||
4654967986887
|
|
||||||
4564679986453
|
|
||||||
1224686865563
|
|
||||||
2546548887735
|
|
||||||
4322674655533
|
|
||||||
"#;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sample_data() {
|
|
||||||
let input = parse(&SAMPLE_DATA);
|
|
||||||
assert_eq!(part1(&input), 102);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,6 +16,5 @@ mod day13;
|
||||||
mod day14;
|
mod day14;
|
||||||
mod day15;
|
mod day15;
|
||||||
mod day16;
|
mod day16;
|
||||||
mod day17;
|
|
||||||
|
|
||||||
aoc_lib! { year = 2023 }
|
aoc_lib! { year = 2023 }
|
||||||
|
|
Loading…
Reference in New Issue