day18: part 2 solution + some optimizations
All checks were successful
test / AoC 2024 (push) Successful in 2m47s

This commit is contained in:
Keenan Tims 2024-12-17 22:13:41 -08:00
parent 414569537e
commit 4f48d839b2
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -1,31 +1,27 @@
use std::{cmp::Reverse, collections::{BinaryHeap, HashMap, VecDeque}}; use std::{cmp::Reverse, collections::BinaryHeap};
use aoc_runner_derive::aoc; use aoc_runner_derive::aoc;
use grid::Grid; use grid::Grid;
#[derive(Clone)]
struct MemoryMap { struct MemoryMap {
map: Grid<bool>, map: Grid<bool>,
byte_stream: Vec<(i64, i64)> byte_stream: Vec<(i64, i64)>,
} }
impl MemoryMap { impl MemoryMap {
fn from_str(input: &str, width: usize, height: usize, n: Option<usize>) -> Self { fn from_str(input: &str, width: usize, height: usize) -> Self {
let mut map = Grid::with_shape(width, height, true); let map = Grid::with_shape(width, height, true);
let mut byte_stream = Vec::new(); let mut byte_stream = Vec::new();
let mut count = 0;
for line in input.lines() { for line in input.lines() {
if let Some((x, y)) = line.split_once(',') { if let Some((x, y)) = line.split_once(',') {
let pos: (i64, i64) = (x.parse().unwrap(), y.parse().unwrap()); let pos: (i64, i64) = (x.parse().unwrap(), y.parse().unwrap());
byte_stream.push(pos); byte_stream.push(pos);
}
count += 1;
if n.is_some_and(|n| count == n) {
break;
} }
} }
Self { map , byte_stream } Self { map, byte_stream }
} }
fn place_byte(&mut self, i: usize) { fn place_byte(&mut self, i: usize) {
let pos = self.byte_stream[i]; let pos = self.byte_stream[i];
@ -33,8 +29,9 @@ impl MemoryMap {
panic!("corruption outside memory bounds"); panic!("corruption outside memory bounds");
} }
} }
fn place_all_bytes(&mut self) { fn place_bytes(&mut self, n: usize) {
for i in 0..self.byte_stream.len() { assert!(n < self.byte_stream.len());
for i in 0..n {
self.place_byte(i); self.place_byte(i);
} }
} }
@ -47,7 +44,7 @@ impl MemoryMap {
.collect() .collect()
} }
fn dijkstra(&self) -> Option<Vec<(i64, i64)>> { fn dijkstra(&self) -> Option<Vec<(i64, i64)>> {
let start = (0i64, 0i64); let start = (0i64, 0i64);
let goal = (self.map.width() as i64 - 1, self.map.height() as i64 - 1); let goal = (self.map.width() as i64 - 1, self.map.height() as i64 - 1);
@ -88,11 +85,16 @@ impl MemoryMap {
} }
pub fn part1_impl(input: &str, width: usize, height: usize, n: usize) -> usize { pub fn part1_impl(input: &str, width: usize, height: usize, n: usize) -> usize {
let mut map = MemoryMap::from_str(input, width, height, Some(n)); let mut map = MemoryMap::from_str(input, width, height);
map.place_all_bytes(); map.place_bytes(n);
let path = map.dijkstra().expect("no path found"); let path = map.dijkstra().expect("no path found");
let mut sol_map = map.map.same_shape(b'.'); let mut sol_map = map.map.same_shape(b'.');
sol_map.data = map.map.data.iter().map(|clear| if *clear { b'.' } else { b'#' }).collect(); sol_map.data = map
.map
.data
.iter()
.map(|clear| if *clear { b'.' } else { b'#' })
.collect();
for visited in &path { for visited in &path {
sol_map.set(visited, b'O'); sol_map.set(visited, b'O');
} }
@ -100,13 +102,21 @@ pub fn part1_impl(input: &str, width: usize, height: usize, n: usize) -> usize {
path.len() - 2 // count vertexes, not visited nodes path.len() - 2 // count vertexes, not visited nodes
} }
pub fn part2_impl(input: &str, width: usize, height: usize) -> (i64, i64) { pub fn part2_impl(input: &str, width: usize, height: usize, n: usize) -> (i64, i64) {
let mut map = MemoryMap::from_str(input, width, height, None); let mut input_map = MemoryMap::from_str(input, width, height);
for byte in 0..map.byte_stream.len() { input_map.place_bytes(n);
map.place_byte(byte); let mut path = input_map.dijkstra().expect("no path found");
if map.dijkstra().is_none() {
return map.byte_stream[byte] for byte in n..input_map.byte_stream.len() {
input_map.place_byte(byte);
if path.contains(&input_map.byte_stream[byte]) {
if let Some(new_path) = input_map.dijkstra() {
path = new_path;
} else {
println!("obstruction found on trial {}", byte);
return input_map.byte_stream[byte];
}
} }
} }
panic!("no bytes block route"); panic!("no bytes block route");
@ -119,7 +129,7 @@ pub fn part1(input: &str) -> usize {
#[aoc(day18, part2)] #[aoc(day18, part2)]
pub fn part2(input: &str) -> String { pub fn part2(input: &str) -> String {
let sol = part2_impl(input, 71, 71); let sol = part2_impl(input, 71, 71, 1024);
format!("{},{}", sol.0, sol.1) format!("{},{}", sol.0, sol.1)
} }
@ -159,6 +169,6 @@ mod tests {
#[test] #[test]
fn part2_example() { fn part2_example() {
assert_eq!(part2_impl(EXAMPLE, 7, 7), (6, 1)); assert_eq!(part2_impl(EXAMPLE, 7, 7, 12), (6, 1));
} }
} }