1
0

day 18 performance improvement

This commit is contained in:
2025-10-21 15:00:51 +02:00
parent 944faf2159
commit 85de6b1539

View File

@@ -1,10 +1,9 @@
use std::{ use std::{collections::VecDeque, fs::read_to_string};
collections::{HashMap, HashSet, VecDeque},
fs::read_to_string,
};
use itertools::Itertools; use itertools::Itertools;
use crate::utils::grid::Grid;
pub fn day_main() { pub fn day_main() {
let input = read_to_string("input/day18.txt").unwrap(); let input = read_to_string("input/day18.txt").unwrap();
let input = input.trim(); let input = input.trim();
@@ -18,30 +17,33 @@ fn part1(input: &str) -> RiddleResult {
solve1(input, 1024, 70) solve1(input, 1024, 70)
} }
fn solve1(input: &str, n: usize, coord_max: i32) -> usize { fn solve1(input: &str, n: usize, coord_max: i64) -> usize {
let points = parse(input); let points = parse(input);
sp(&points[..n], coord_max).unwrap() sp(&points[..n], coord_max).unwrap()
} }
fn sp(points: &[(i32, i32)], coord_max: i32) -> Option<usize> { fn sp(points: &[(i64, i64)], coord_max: i64) -> Option<usize> {
let pointss: HashSet<&(i32, i32)> = HashSet::from_iter(points); let mut pcheck = Grid::from_default(coord_max + 1, coord_max + 1);
let mut visited = HashMap::new(); for p in points {
pcheck.set(*p, true);
}
let mut visited = Grid::from_default(coord_max + 1, coord_max + 1);
let mut queue = VecDeque::from_iter([(0, 0, 0)]); let mut queue = VecDeque::from_iter([(0, 0, 0)]);
while let Some((x, y, c)) = queue.pop_front() { while let Some((x, y, c)) = queue.pop_front() {
if x == coord_max && y == coord_max { if x == coord_max && y == coord_max {
return Some(c); return Some(c);
} }
if visited.contains_key(&(x, y)) { if visited.get((x, y)) == Some(&true) {
continue; continue;
} }
visited.insert((x, y), c); visited.set((x, y), true);
for (dx, dy) in [(0, 1), (1, 0), (0, -1), (-1, 0)] { for (dx, dy) in [(0, 1), (1, 0), (0, -1), (-1, 0)] {
let (a, b) = (x + dx, y + dy); let (a, b) = (x + dx, y + dy);
if (0..=coord_max).contains(&a) if (0..=coord_max).contains(&a)
&& (0..=coord_max).contains(&b) && (0..=coord_max).contains(&b)
&& !pointss.contains(&(a, b)) && pcheck.get((a, b)) == Some(&false)
{ {
queue.push_back((a, b, c + 1)); queue.push_back((a, b, c + 1));
} }
@@ -50,7 +52,7 @@ fn sp(points: &[(i32, i32)], coord_max: i32) -> Option<usize> {
None None
} }
fn parse(input: &str) -> Vec<(i32, i32)> { fn parse(input: &str) -> Vec<(i64, i64)> {
input input
.trim() .trim()
.lines() .lines()
@@ -67,7 +69,7 @@ fn part2(input: &str) -> String {
solve2(input, 1024, 70) solve2(input, 1024, 70)
} }
fn solve2(input: &str, fixed: usize, max_coord: i32) -> String { fn solve2(input: &str, fixed: usize, max_coord: i64) -> String {
let points = parse(input); let points = parse(input);
// we want the first point with which there is no shortest path // we want the first point with which there is no shortest path