[Rust/2023/23] Add solution

This commit is contained in:
Felix Bargfeldt 2023-12-24 18:38:28 +01:00
parent 91f216eaa9
commit 8610678124
Signed by: Defelo
GPG key ID: 2A05272471204DD3
8 changed files with 165 additions and 7 deletions

File diff suppressed because one or more lines are too long

117
Rust/2023/23.rs Normal file
View file

@ -0,0 +1,117 @@
#![feature(test)]
use aoc::{grid::Direction, iter_ext::IterExt, tuples::TupleExt};
use indexmap::IndexMap;
use itertools::Itertools;
use rayon::prelude::*;
use rustc_hash::FxHashMap;
type Input = Vec<Vec<Tile>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Tile {
Path,
Forest,
Slope(Direction),
}
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
line.bytes()
.map(|b| match b {
b'.' => Tile::Path,
b'#' => Tile::Forest,
b => Tile::Slope(b.into()),
})
.collect()
})
.collect()
}
fn trace_path<const IGNORE_SLOPES: bool>(
input: &Input,
p: (usize, usize),
d: Direction,
) -> impl Iterator<Item = (usize, usize)> + '_ {
let (w, h) = (input[0].len(), input.len());
std::iter::successors(Some((p, d)), move |&(p, d)| {
[d.rotate_left(), d, d.rotate_right()]
.into_iter()
.filter(|&dir| IGNORE_SLOPES || !matches!(input[p.1][p.0], Tile::Slope(s) if s != dir))
.filter_map(|dir| dir.step(p.0, p.1, w, h).map(|q| (q, dir)))
.find(|&((x, y), _)| input[y][x] != Tile::Forest)
})
.map(|(p, _)| p)
}
fn longest_path(junctions: &[FxHashMap<usize, usize>], p: usize, visited: u64) -> Option<usize> {
if p == 1 {
Some(0)
} else if visited.count_ones() < 10 {
junctions[p]
.par_iter()
.filter(|&(&q, _)| visited & (1 << q) == 0)
.filter_map(|(&q, &d)| longest_path(junctions, q, visited | (1 << p)).map(|x| x + d))
.max()
} else {
junctions[p]
.iter()
.filter(|&(&q, _)| visited & (1 << q) == 0)
.filter_map(|(&q, &d)| longest_path(junctions, q, visited | (1 << p)).map(|x| x + d))
.max()
}
}
fn solve<const IGNORE_SLOPES: bool>(input: &Input) -> usize {
let (w, h) = (input[0].len(), input.len());
let start = (input[0].iter().index(&Tile::Path).unwrap(), 0);
let end = (input[h - 1].iter().index(&Tile::Path).unwrap(), h - 1);
let junction_ids = [start, end]
.into_iter()
.chain(
(0..w)
.flat_map(|x| (0..h).map(move |y| (x, y)))
.filter(|&(x, y)| input[y][x] == Tile::Path)
.filter(|&(x, y)| {
Direction::iter()
.filter_map(|d| d.step(x, y, w, h))
.filter(|&(x, y)| input[y][x] != Tile::Forest)
.count()
> 2
}),
)
.enumerate()
.map(TupleExt::swap)
.collect::<IndexMap<(usize, usize), usize>>();
let junctions = junction_ids
.iter()
.map(|(&p, _)| {
Direction::iter()
.filter_map(|d| {
let p = d
.step(p.0, p.1, w, h)
.filter(|&(x, y)| input[y][x] != Tile::Forest)?;
trace_path::<IGNORE_SLOPES>(input, p, d)
.enumerate()
.find_map(|(dist, p)| junction_ids.get(&p).map(|&m| (m, dist + 1)))
})
.collect()
})
.collect_vec();
longest_path(&junctions, 0, 0).unwrap()
}
fn part1(input: &Input) -> usize {
solve::<false>(input)
}
fn part2(input: &Input) -> usize {
solve::<true>(input)
}
aoc::main!(2023, 23, ex: 1);

View file

@ -292,5 +292,8 @@ path = "2023/21.rs"
name = "2023_22"
path = "2023/22.rs"
[[bin]]
name = "2023_23"
path = "2023/23.rs"
[[bin]]
name = "2023_24"
path = "2023/24.rs"

View file

@ -62,7 +62,7 @@ impl Direction {
}
}
pub fn iter() -> impl Iterator<Item = Self> {
pub fn iter() -> impl Iterator<Item = Self> + Clone {
[Self::North, Self::East, Self::South, Self::West].into_iter()
}
}
@ -70,11 +70,17 @@ impl Direction {
impl From<char> for Direction {
fn from(c: char) -> Self {
match c {
'U' | 'N' => Self::North,
'R' | 'E' => Self::East,
'D' | 'S' => Self::South,
'L' | 'W' => Self::West,
'U' | 'N' | '^' => Self::North,
'R' | 'E' | '>' => Self::East,
'D' | 'S' | 'v' => Self::South,
'L' | 'W' | '<' => Self::West,
_ => panic!(),
}
}
}
impl From<u8> for Direction {
fn from(c: u8) -> Self {
char::from(c).into()
}
}

View file

@ -25,6 +25,13 @@ pub trait IterExt: Iterator {
where
Self: Sized,
P: FnMut(Self::Item) -> bool;
fn index(mut self, elem: impl PartialEq<Self::Item>) -> Option<usize>
where
Self: Sized,
{
self.position(|x| elem == x)
}
}
impl<I> IterExt for I

23
examples/2023/23/1 Normal file
View file

@ -0,0 +1,23 @@
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#

1
examples/2023/23/1.1 Normal file
View file

@ -0,0 +1 @@
94

1
examples/2023/23/1.2 Normal file
View file

@ -0,0 +1 @@
154