Optimize day18
This commit is contained in:
parent
1e4a334908
commit
2807f51648
110
day18/Cargo.lock
generated
110
day18/Cargo.lock
generated
@ -2,15 +2,125 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "day18"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"glam",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.29.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc46dd3ec48fdd8e693a98d2b8bafae273a2d54c1de02a2a7e3d57d501f39677"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.168"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.90"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
@ -4,4 +4,5 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ahash = "0.8.11"
|
||||
glam = "0.29.2"
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
use std::{cmp::Reverse, collections::BinaryHeap};
|
||||
use std::{
|
||||
cmp::Reverse,
|
||||
collections::BinaryHeap,
|
||||
};
|
||||
|
||||
use ahash::AHashSet;
|
||||
use glam::IVec2;
|
||||
|
||||
enum Direction {
|
||||
@ -24,42 +28,6 @@ impl Direction {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||
enum Tile {
|
||||
Corrupted,
|
||||
Safe,
|
||||
}
|
||||
use Tile::*;
|
||||
|
||||
fn parse(input: &str) -> Vec<IVec2> {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let (y, x) = l.split_once(',').unwrap();
|
||||
IVec2 {
|
||||
x: x.parse().unwrap(),
|
||||
y: y.parse().unwrap(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn build_grid(dimensions: &IVec2, bytes_amount: usize, positions: &[IVec2]) -> Vec<Vec<Tile>> {
|
||||
let mut grid = vec![];
|
||||
for px in 0..=dimensions.x {
|
||||
let mut line = vec![];
|
||||
for py in 0..=dimensions.y {
|
||||
if positions[..bytes_amount].contains(&IVec2::new(px, py)) {
|
||||
line.push(Corrupted);
|
||||
} else {
|
||||
line.push(Safe);
|
||||
}
|
||||
}
|
||||
grid.push(line);
|
||||
}
|
||||
grid
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||
struct Node {
|
||||
score: u32,
|
||||
@ -78,67 +46,102 @@ impl PartialOrd for Node {
|
||||
}
|
||||
}
|
||||
|
||||
fn find_path(grid: &[Vec<Tile>], start_pos: &IVec2, end_pos: &IVec2) -> u32 {
|
||||
let mut distances = vec![vec![u32::MAX; grid[0].len()]; grid.len()];
|
||||
type Distance = (u32, Vec<IVec2>);
|
||||
|
||||
fn parse(input: &str) -> Vec<IVec2> {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let (y, x) = l.split_once(',').unwrap();
|
||||
IVec2 {
|
||||
x: x.parse().unwrap(),
|
||||
y: y.parse().unwrap(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_score<'a>(scores: &'a mut [Vec<Distance>], pos: &IVec2) -> &'a mut Distance {
|
||||
&mut scores[pos.x as usize][pos.y as usize]
|
||||
}
|
||||
|
||||
fn find_path(corrupt_pos: &AHashSet<IVec2>, dimensions: &IVec2) -> (Vec<IVec2>, u32) {
|
||||
let mut scores =
|
||||
vec![vec![(u32::MAX, vec![]); dimensions.y as usize + 1]; dimensions.x as usize + 1];
|
||||
let mut to_visit = BinaryHeap::new();
|
||||
|
||||
distances[start_pos.x as usize][start_pos.y as usize] = 0;
|
||||
let start_pos = IVec2::new(0, 0);
|
||||
|
||||
get_score(&mut scores, &start_pos).0 = 0;
|
||||
to_visit.push(Reverse(Node {
|
||||
score: 0,
|
||||
position: *start_pos,
|
||||
position: start_pos,
|
||||
}));
|
||||
|
||||
while let Some(Reverse(curr)) = to_visit.pop() {
|
||||
if curr.position == *end_pos {
|
||||
return curr.score;
|
||||
if curr.position == *dimensions {
|
||||
return (get_score(&mut scores, &curr.position).1.clone(), curr.score);
|
||||
}
|
||||
|
||||
if curr.score > distances[curr.position.x as usize][curr.position.y as usize] {
|
||||
if curr.score > get_score(&mut scores, &curr.position).0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
for direction in Direction::all() {
|
||||
let new_pos = curr.position + direction.to_vec();
|
||||
let Some(Some(t)) = grid
|
||||
.get(new_pos.x as usize)
|
||||
.map(|l| l.get(new_pos.y as usize))
|
||||
else {
|
||||
if new_pos.cmplt(start_pos).any()
|
||||
|| new_pos.cmpgt(*dimensions).any()
|
||||
|| corrupt_pos.contains(&new_pos)
|
||||
{
|
||||
continue;
|
||||
};
|
||||
if *t != Corrupted {
|
||||
let new_score = curr.score + 1;
|
||||
if new_score < distances[new_pos.x as usize][new_pos.y as usize] {
|
||||
distances[new_pos.x as usize][new_pos.y as usize] = new_score;
|
||||
to_visit.push(Reverse(Node {
|
||||
score: new_score,
|
||||
position: new_pos,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
let new_score = curr.score + 1;
|
||||
if new_score < get_score(&mut scores, &new_pos).0 {
|
||||
get_score(&mut scores, &new_pos).0 = new_score;
|
||||
|
||||
let mut prev_positions = get_score(&mut scores, &curr.position).1.clone();
|
||||
prev_positions.push(curr.position);
|
||||
get_score(&mut scores, &new_pos).1.extend(prev_positions);
|
||||
|
||||
to_visit.push(Reverse(Node {
|
||||
score: new_score,
|
||||
position: new_pos,
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
u32::MAX
|
||||
(vec![], u32::MAX)
|
||||
}
|
||||
|
||||
fn part1(positions: &[IVec2], dimensions: &IVec2) -> u32 {
|
||||
let grid = build_grid(dimensions, 1024, positions);
|
||||
find_path(&grid, &IVec2::new(0, 0), dimensions)
|
||||
find_path(
|
||||
&AHashSet::from_iter(positions[..1024].iter().map(|i| i.to_owned())),
|
||||
dimensions,
|
||||
)
|
||||
.1
|
||||
}
|
||||
|
||||
fn part2(positions: &[IVec2], dimensions: &IVec2) -> String {
|
||||
let start_pos = IVec2::new(0, 0);
|
||||
// todo: use binary search
|
||||
for i in 1024.. {
|
||||
let grid = build_grid(dimensions, i, positions);
|
||||
if find_path(&grid, &start_pos, dimensions) == u32::MAX {
|
||||
let mut prev_path = AHashSet::new();
|
||||
for i in 1024..positions.len() {
|
||||
if !prev_path.is_empty() && !prev_path.contains(&positions[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
let (path, len) = find_path(
|
||||
&AHashSet::from_iter(positions[..i].iter().map(|i| i.to_owned())),
|
||||
dimensions,
|
||||
);
|
||||
prev_path = AHashSet::from_iter(path);
|
||||
if len == u32::MAX {
|
||||
return positions
|
||||
.get(i - 1)
|
||||
.map(|p| format!("{},{}", p.y, p.x))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
panic!("not found")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user