From: alex <> Date: Wed, 18 Dec 2024 09:14:49 +0000 (+0100) Subject: Day18 - part 2 X-Git-Url: https://aoc.elinar.fr/?a=commitdiff_plain;h=2daca6b9154fea79cbb7c62594501ad2fe547892;p=aoc_2024 Day18 - part 2 --- diff --git a/src/day18.rs b/src/day18.rs index ebb8cab..f7bb339 100644 --- a/src/day18.rs +++ b/src/day18.rs @@ -7,7 +7,7 @@ const WIDTH: isize = 70; struct Puzzle { mem: HashSet<(isize, isize)>, - corrupted: HashMap<(isize, isize), usize>, + corrupted: Vec<(isize, isize)>, height: isize, width: isize, start: (isize, isize), @@ -16,13 +16,17 @@ struct Puzzle { impl Puzzle { pub fn new(input: &str, height: isize, width: isize) -> Self { - let mem: HashSet<(isize, isize)> = HashSet::new(); - let corrupted: HashMap<(isize, isize), usize> = input.lines() - .enumerate() - .map(|(i, l)| { + let mut mem: HashSet<(isize, isize)> = HashSet::new(); + (0..=height).for_each(|row| { + (0..=width).for_each(|col| { + mem.insert((row, col)); + }); + }); + let corrupted: Vec<(isize, isize)> = input.lines() + .map(|l| { let (x, y) = l.split_once(",").unwrap(); let (x, y) = (y.parse::().unwrap(), x.parse::().unwrap()); - ((y, x), i) + (y, x) }) .collect(); let start = (0, 0); @@ -30,16 +34,10 @@ impl Puzzle { Self { mem, corrupted, height, width, start, end } } - fn build_mem(&mut self, corrupted_size: usize) { - self.mem = HashSet::new(); - (0..=self.height).for_each(|row| { - (0..=self.width).for_each(|col| { - if !self.corrupted.contains_key(&(row, col)) || - *self.corrupted.get(&(row,col)).unwrap() >= corrupted_size { - self.mem.insert((row, col)); - } - }); - }); + fn corrupt(&mut self, id: usize) { + if let Some(pos) = self.corrupted.get(id) { + self.mem.remove(&pos); + } } fn steps(&self) -> u32 { @@ -74,30 +72,45 @@ impl Puzzle { nb.cmp(&na) }); } - *visited.get(&self.end).unwrap() + match visited.get(&self.end) { + Some(dst) => *dst, + None => 0 + } } } fn run_part1(input: &str) -> Result> { println!("Running {} - part 1", get_day()); - let corrupted_size = 1024; let mut puzzle = Puzzle::new(input, HEIGHT, WIDTH); - puzzle.build_mem(corrupted_size); + for i in 0..1024 { + puzzle.corrupt(i); + } Ok(puzzle.steps()) } -fn run_part2(input: &str) -> Result> { +fn run_part2(input: &str, height: isize, width: isize, corrupt_id: usize) -> Result> { println!("Running {} - part 2", get_day()); - Ok(0) + let mut puzzle = Puzzle::new(input, height, width); + let mut id = 0; + while id < corrupt_id { + puzzle.corrupt(id); + id += 1; + } + while puzzle.steps() > 0 { + id += 1; + puzzle.corrupt(id); + } + let pos = puzzle.corrupted.get(id).unwrap(); + Ok(format!("{},{}", pos.0, pos.1)) } pub fn run(input: &str) -> Result<(), Box> { let res = run_part1(input)?; println!("{res}"); - let res = run_part2(input)?; + let res = run_part2(input, HEIGHT, WIDTH, 1024)?; println!("{res}"); Ok(()) @@ -142,13 +155,14 @@ mod tests { #[test] fn test_part1() { let mut puzzle = Puzzle::new(TEXT_INPUT, 6, 6); - let corrupted_size = 12; - puzzle.build_mem(corrupted_size); + for i in 0..12 { + puzzle.corrupt(i); + } assert_eq!(22, puzzle.steps()); } #[test] fn test_part2() { - assert_eq!(0, run_part2(TEXT_INPUT).unwrap()); + assert_eq!("6,1", run_part2(TEXT_INPUT, 6, 6, 12).unwrap()); } }