Compare commits

...

3 Commits

Author SHA1 Message Date
1c254fff93
Revert "cargo: optimize harder"
All checks were successful
test / AoC 2024 (push) Successful in 1m50s
This reverts commit 44108c4b35a93664be800872359de5dd72186fa3.
2024-12-18 00:53:51 -08:00
44108c4b35
cargo: optimize harder 2024-12-18 00:41:40 -08:00
b588837624
day18: small optimization - avoid duplicates
not sure if the input contains dupes, but avoid doing a second path
search if the square already contains a byte, seems like a small
performance gain
2024-12-18 00:41:28 -08:00

View File

@ -51,10 +51,12 @@ impl MemoryMap {
Self { map, byte_stream }
}
fn place_byte(&mut self, i: usize) {
// Return if the byte caused a new blockage or not
fn place_byte(&mut self, i: usize) -> bool {
let pos = self.byte_stream[i];
if self.map.set(&pos, false).is_none() {
panic!("corruption outside memory bounds");
match self.map.set(&pos, false) {
None => panic!("corruption outside memory bounds"),
Some(x) => x,
}
}
fn place_bytes(&mut self, n: usize) {
@ -126,11 +128,10 @@ pub fn part2_impl(input: &str, width: usize, height: usize, n: usize) -> (i64, i
input_map.place_bytes(n);
let mut path = input_map.dijkstra::<Vec<(i64, i64)>>((0, 0)).expect("no path found");
println!("{:?}", path);
for byte in n..input_map.byte_stream.len() {
input_map.place_byte(byte);
if input_map.place_byte(byte) {
// If it's a new blockage, and it obstructs our best path, we need to do a new path search
if let Some((obs_at, _)) = path.iter().find_position(|v| *v == &input_map.byte_stream[byte]) {
let (before, _) = path.split_at(obs_at);
@ -141,6 +142,7 @@ pub fn part2_impl(input: &str, width: usize, height: usize, n: usize) -> (i64, i
}
}
}
}
panic!("no bytes block route");
}