This commit is contained in:
Moritz Hölting 2023-12-11 13:00:45 +01:00
parent c9d3344d05
commit 68ee92a178
6 changed files with 745 additions and 1 deletions

279
Cargo.lock generated
View File

@ -14,6 +14,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "console"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"unicode-width",
"windows-sys",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
@ -120,18 +133,106 @@ dependencies = [
"itertools",
]
[[package]]
name = "day-11"
version = "0.0.0"
dependencies = [
"indicatif",
"indoc",
"pathfinding",
"rayon",
]
[[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",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[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 = "indicatif"
version = "0.17.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25"
dependencies = [
"console",
"instant",
"number_prefix",
"portable-atomic",
"rayon",
"unicode-width",
]
[[package]]
name = "indoc"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8"
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "integer-sqrt"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770"
dependencies = [
"num-traits",
]
[[package]]
name = "itertools"
version = "0.12.0"
@ -141,6 +242,18 @@ dependencies = [
"either",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "memchr"
version = "2.6.4"
@ -248,6 +361,51 @@ dependencies = [
"autocfg",
]
[[package]]
name = "number_prefix"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]]
name = "pathfinding"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "752866d4511516a35883728309499db42696f388263586b70659a5461e641db5"
dependencies = [
"deprecate-until",
"fixedbitset",
"indexmap",
"integer-sqrt",
"num-traits",
"rustc-hash",
"thiserror",
]
[[package]]
name = "portable-atomic"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0"
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ranges"
version = "0.3.3"
@ -274,8 +432,129 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "semver"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]]
name = "syn"
version = "2.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13fa70a4ee923979ffb522cacce59d34421ebdea5625e1073c4326ef9d2dd42e"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"

View File

@ -9,7 +9,8 @@ members = [
"day-07",
"day-08",
"day-09",
"day-10"
"day-10",
"day-11"
]
[workspace.dependencies]

14
day-11/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "day-11"
version = "0.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
indicatif = { version = "0.17.7", features = ["rayon"] }
pathfinding = "4.4.0"
rayon.workspace = true
[dev-dependencies]
indoc.workspace = true

140
day-11/src/bin/input.txt Normal file
View File

@ -0,0 +1,140 @@
.....................................................................................#......#...............................................
..#.....#.................................................................#........................#........................................
...............................#...............................................................................#.....#......................
.................................................#.............#...............................#...................................#........
..............#......#.................#...............#............#.............#..........................................#..............
.........................................................................................#.................#...............................#
.#..........................................................................................................................................
..........................#......#............................................#......#.................................#....................
.................................................................................................#..........................................
.........................................#..........#..........#............................................................#...............
......#.....#......#..........................#........................#....................................................................
....................................#.........................................................................#..........................#..
..................................................................................#.........................................................
...................................................................................................................#........................
.................................................................#........................................#.................................
.............................#..............................#..........................#.......#............................................
..........................................#.....#.............................#.............................................................
#..................#..................................................................................................................#.....
....................................#...................#..........................#...........................#..............#.............
........#................#...................#.............................................................................................#
...............................#....................#...........................................#...........................................
.......................................#..............................................#.....................................................
........................................................................#........#...........................#..............................
..................................#........#...................#........................................#.........#.................#.......
#................#........................................................................................................#.................
.........#.......................................#...............................................#.............................#........#...
...............................................................................................................#............................
.........................................................................................#..................................................
...........................................................#..................................#..........#...........................#......
......#.........................#..........................................................................................#................
....................#.....................#.......#...............................#.................................#.......................
..........................#..............................................#..................................................................
..........#..........................#.......................#...................................#..............#..............#...........#
......................................................#.....................................................................................
............................................................................................................................................
#...............................................#................#............................#.............................................
..................#......................#............................#................................#................................#...
............................................................................................................................................
....................................................#....................................#.............................#....................
..............#...................#................................#..........#..............................#.............................#
............................#...............................................................................................#...............
....#.................#.....................................#..........#............................................................#.......
......................................................#.....................................................................................
...........#..............................#.................................................#..........#.................#..................
.................................#..............#................#..........#.......#.......................................................
...................................................................................................................#..............#.........
#...........................................................................................................#...............................
........#.......#............................................................................................................#..............
............................#.....................#..........#......................................#.....................................#.
.....................................#.................................................#........................#...........................
.........................................................................................................................#...........#......
.......................#.............................#........................#.............................................................
..............#...............................#.................#..................#........#...............................................
...#......................................................................#.................................#..............................#
...................#..................#.............................................................................#........#..............
............................................................................................................................................
.........................#.........................#........................................................................................
......#......#.............................................#.............................#............#.....................................
.................................#......................................#...................................................................
.........................................#........................#..............................#.................#..............#.........
............................................................................................................................................
......................................................#.................................................#..............#..................#.
......................#......#...............#...................................#............................#.............................
....#...........#.................#............................................................#...............................#............
..........................................................................#..........................#......................................
..........................................#................................................#...............................#............#...
................................................................................................................#...........................
..........#....................................#...................................................................................#........
................................#....................#..............................................................#.......................
......#.......................................................#...............................#.............................................
#.......................#.....................................................................................................#.............
.................#...................#...........................................#..........................#...............................
.....................................................................#....................#.................................................
..............................#.................#.......#.........................................#.........................................
..#........................................................................#....................................#.........#.................
...........#............................#..........................................#.............................................#..........
......#.............................................................................................................#.....................#.
........................................................................................................#...................................
..............#...................#.........#...............................................................................................
........................................................#.............#..........#.....#..........#............................#............
.........#......................................................#...........................#...............................................
.........................................#...................................#.................................#............................
............................................................................................................................................
................#...................................................................#.................#..................#..................
.................................#..........................#.............#....................#................................#..........#
...#........................#.........................................................................................................#.....
.................................................#......................................#.....................#......#......................
...........................................#.........................#........#.............................................................
........#......#...................................................................................................................#.....#..
.#..................#..................................#.........#..........................................................................
..................................#.....#.................................#.................................................................
...................................................#.................................#......................................................
............................................................#...............................................................................
.............................................#......................#..........#............#.........................#......#..............
................................#...................................................................#.......................................
.....................................................#..................#......................................#............................
...#......................#.....................................................................................................#...........
...........#...........................................................................#..............................................#.....
................#..................................................#..............................................#.........................
.......................#......................................................................#.............................................
................................#........#.........#.....................#.....#......................#.....................................
.#..........................................................................................................................................
........................................................#................................#...................#..................#...........
.....................................#........#.................................................#.......................................#...
.........#...........#......................................................................................................................
....................................................#.............................................................#.................#.......
.............#............#........................................#.............#.....................#...................#...............#
..................#....................#.................................#.............#....................................................
................................................#...........................................................................................
.......................#...............................................................................................#....................
....#..........................................................#.....#..............#...........................................#...........
...............................................................................................#..................#.......................#.
.........#...................#............................................#.................................................................
.............................................................................................................#..............#......#........
...........................................................................................#................................................
.#...........................................................#..............................................................................
..............#..............................#........................................#.............#.......................................
....................................#..............#..............#.........#.............................#.............#.............#.....
....................#............................................................................................#..........................
.........................................................................................#..................................................
...........#............................#................#....................................................................#.............
.....#......................................................................................................#...............................
..................................#................................................#........#.............................................#.
.#................#......#...........................................................................#......................................
..............................................#.......................#................#............................................#.......
.........#............................#........................#..........................................#.....#......#....................
..............................................................................................#.............................................
........................................................................................................................................#...
..........................#........................#...............#................................#............................#..........
..#........................................#............#............................................................#......................
...............................#.....#........................................#.......#......................................#..............
.......................................................................................................#....................................
.....................................................#..............................................................................#.......
........#......................................#...........................#.............................................#.................#
...................#.....#......................................#.............................#.............................................
............#.........................#......................................................................#.......#.................#....
..............................#...................................................#.........................................................
............................................#.........................................................#.....................................
.......#..........................#................#.....#.............#....................................................................
..................#.........................................................#.............#.......................................#.........

146
day-11/src/bin/part1.rs Normal file
View File

@ -0,0 +1,146 @@
use indicatif::ParallelProgressIterator;
use pathfinding::prelude::bfs;
use rayon::prelude::*;
fn main() {
println!("{}", part1(include_str!("./input.txt")));
}
fn part1(input: &str) -> usize {
let mut lines = input
.lines()
.flat_map(|l| {
let row = l
.chars()
.map(|c| Space::try_from(c).expect("invalid char"))
.collect::<Vec<_>>();
if row.iter().all(|s| *s == Space::Empty) {
vec![row.clone(), row]
} else {
vec![row]
}
})
.collect::<Vec<_>>();
let height = lines.len();
let width = lines[0].len();
for i in (0..width).rev() {
if (0..height).all(|j| lines.get(j).unwrap().get(i).unwrap() == &Space::Empty) {
(0..height).for_each(|j| {
lines[j].insert(i, Space::Empty);
});
}
}
let height = lines.len();
let width = lines[0].len();
let galaxies = lines
.par_iter()
.enumerate()
.flat_map(|(y, l)| {
l.par_iter()
.enumerate()
.filter_map(move |(x, s)| (s == &Space::Galaxy).then_some((x, y)))
})
.collect::<Vec<_>>();
let pairs = galaxies
.par_iter()
.enumerate()
.flat_map(|(i, (x1, y1))| {
galaxies
.par_iter()
.enumerate()
.filter_map(move |(j, (x2, y2))| {
if j > i {
Some(((*x1, *y1), (*x2, *y2)))
} else {
None
}
})
})
.collect::<Vec<_>>();
let distances = pairs
.into_par_iter()
.progress()
.filter_map(|(start, target)| {
bfs(
&start,
|coords| successors(coords, &target, width, height),
|cur| *cur == target,
)
.map(|path| path.len() - 1)
})
.collect::<Vec<_>>();
distances.iter().sum()
}
fn successors(
(x, y): &(usize, usize),
(tx, ty): &(usize, usize),
width: usize,
height: usize,
) -> Vec<(usize, usize)> {
let mut neighbors = vec![];
if tx < x && *x > 0 {
neighbors.push((*x - 1, *y));
}
if tx > x && *x < width - 1 {
neighbors.push((*x + 1, *y));
}
if ty < y && *y > 0 {
neighbors.push((*x, *y - 1));
}
if ty > y && *y < height - 1 {
neighbors.push((*x, *y + 1));
}
neighbors
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Space {
Empty,
Galaxy,
}
impl TryFrom<char> for Space {
type Error = ();
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'.' => Ok(Self::Empty),
'#' => Ok(Self::Galaxy),
_ => Err(()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
#[test]
fn test_part1() {
assert_eq!(
part1(indoc!(
"
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
"
)),
374
);
}
}

164
day-11/src/bin/part2.rs Normal file
View File

@ -0,0 +1,164 @@
use indicatif::ParallelProgressIterator;
use pathfinding::prelude::bfs;
use rayon::prelude::*;
fn main() {
println!("{}", part2(include_str!("./input.txt"), 1_000_000));
}
fn part2(input: &str, factor: usize) -> usize {
let mut lines = input
.lines()
.map(|l| {
let row = l
.chars()
.map(|c| Space::try_from(c).expect("invalid char"))
.collect::<Vec<_>>();
if row.iter().all(|s| *s == Space::Empty) {
row.into_par_iter()
.map(|_| Space::ExpandedEmpty)
.collect::<Vec<_>>()
} else {
row
}
})
.collect::<Vec<_>>();
let height = lines.len();
let width = lines[0].len();
for i in (0..width).rev() {
if (0..height).all(|j| {
matches!(
lines.get(j).unwrap().get(i).unwrap(),
Space::Empty | Space::ExpandedEmpty
)
}) {
(0..height).for_each(|j| {
if let Some(s) = lines[j].get_mut(i) {
*s = Space::ExpandedEmpty;
}
});
}
}
let galaxies = lines
.par_iter()
.enumerate()
.flat_map(|(y, l)| {
l.par_iter()
.enumerate()
.filter_map(move |(x, s)| (s == &Space::Galaxy).then_some((x, y)))
})
.collect::<Vec<_>>();
let pairs = galaxies
.par_iter()
.enumerate()
.flat_map(|(i, (x1, y1))| {
galaxies
.par_iter()
.enumerate()
.filter_map(move |(j, (x2, y2))| {
if j > i {
Some(((*x1, *y1), (*x2, *y2)))
} else {
None
}
})
})
.collect::<Vec<_>>();
let distances = pairs
.into_par_iter()
.progress()
.filter_map(|(start, target)| {
bfs(
&start,
|coords| successors(coords, &target, width, height),
|cur| *cur == target,
)
.map(|path| {
path.iter()
.map(|(x, y)| {
let space = lines.get(*y).unwrap().get(*x).unwrap();
if matches!(space, Space::ExpandedEmpty) {
factor
} else {
1
}
})
.sum::<usize>()
- 1
})
})
.collect::<Vec<_>>();
distances.iter().sum()
}
fn successors(
(x, y): &(usize, usize),
(tx, ty): &(usize, usize),
width: usize,
height: usize,
) -> Vec<(usize, usize)> {
let mut neighbors = vec![];
if tx < x && *x > 0 {
neighbors.push((*x - 1, *y));
}
if tx > x && *x < width - 1 {
neighbors.push((*x + 1, *y));
}
if ty < y && *y > 0 {
neighbors.push((*x, *y - 1));
}
if ty > y && *y < height - 1 {
neighbors.push((*x, *y + 1));
}
neighbors
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Space {
Empty,
ExpandedEmpty,
Galaxy,
}
impl TryFrom<char> for Space {
type Error = ();
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'.' => Ok(Self::Empty),
'#' => Ok(Self::Galaxy),
_ => Err(()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
#[test]
fn test_part2() {
const INPUT: &str = indoc!(
"
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
"
);
assert_eq!(part2(INPUT, 10), 1030);
assert_eq!(part2(INPUT, 100), 8410);
}
}