From 5845e117722f4bfaba2321f9abe7831dc262ffe2 Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Mon, 9 Dec 2024 14:15:01 +0100 Subject: [PATCH] Clean up code --- day9/src/main.rs | 92 ++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 65 deletions(-) diff --git a/day9/src/main.rs b/day9/src/main.rs index de0b802..d361eea 100644 --- a/day9/src/main.rs +++ b/day9/src/main.rs @@ -1,86 +1,48 @@ -use std::{cmp::Ordering, fmt::Display}; - -#[derive(Debug, Clone, Eq, PartialEq)] -struct Space { - id: Option, -} - -impl Display for Space { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - if let Some(i) = self.id { - i.to_string() - } else { - ".".to_string() - } - ) - } -} - -impl PartialOrd for Space { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for Space { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - if self.id.is_none() { - return Ordering::Less; - } - if other.id.is_none() { - return Ordering::Greater; - } - self.id.unwrap().cmp(&other.id.unwrap()) - } -} - -fn parse(input: &str) -> Vec { +fn parse(input: &str) -> Vec { input .chars() .enumerate() .filter(|(_, c)| c.is_ascii_digit()) .flat_map(|(c_idx, c)| { - (0..c.to_digit(10).unwrap()).map(move |_| Space { - id: if c_idx % 2 == 0 { - Some(c_idx / 2) + (0..c.to_digit(10).unwrap()).map(move |_| { + if c_idx % 2 == 0 { + c_idx as isize / 2 } else { - None - }, + -1 + } }) }) .collect() } -fn checksum(filesystem: &[Space]) -> usize { +fn checksum(filesystem: &[isize]) -> usize { filesystem .iter() .enumerate() - .map(|(c_idx, c)| if let Some(v) = c.id { c_idx * v } else { 0 }) + .map(|(c_idx, c)| if c >= &0 { c_idx * *c as usize } else { 0 }) .sum() } -fn part1(filesystem: &mut [Space]) -> usize { +fn part1(filesystem: &mut [isize]) -> usize { for space_idx in 0..filesystem.len() { - if filesystem[space_idx].id.is_none() { - let last = filesystem - .iter() - .enumerate() - .rev() - .find(|(_, n)| n.id.is_some()) - .unwrap(); - if last.0 < space_idx { - break; - } - filesystem.swap(space_idx, last.0); - }; + if filesystem[space_idx] >= 0 { + continue; + } + let last = filesystem + .iter() + .enumerate() + .rev() + .find(|(_, n)| n >= &&0) + .unwrap(); + if last.0 < space_idx { + break; + } + filesystem.swap(space_idx, last.0); } checksum(filesystem) } -fn get_chunk_size(filesystem: &[Space], idx: usize) -> usize { +fn get_chunk_size(filesystem: &[isize], idx: usize) -> usize { let mut chunk_size = 0; while idx + chunk_size < filesystem.len() && filesystem[idx + chunk_size] == filesystem[idx] { chunk_size += 1; @@ -88,17 +50,17 @@ fn get_chunk_size(filesystem: &[Space], idx: usize) -> usize { chunk_size } -fn part2(filesystem: &mut [Space]) -> usize { - for move_id in (0..=filesystem.iter().max().unwrap().id.unwrap()).rev() { +fn part2(filesystem: &mut [isize]) -> usize { + for move_id in (0..=*filesystem.iter().max().unwrap()).rev() { let move_idx = filesystem .iter() .enumerate() - .find(|(_, c)| c.id.unwrap_or(move_id + 1) == move_id) + .find(|(_, c)| c == &&move_id) .unwrap() .0; let move_chunk_size = get_chunk_size(filesystem, move_idx); for free_idx in 0..move_idx { - if filesystem[free_idx].id.is_some() { + if filesystem[free_idx] >= 0 { continue; } if get_chunk_size(filesystem, free_idx) >= move_chunk_size {