Cleaned up the day 9 code a little bit

This commit is contained in:
2024-12-09 22:11:55 +01:00
parent 5be2ca974e
commit 50c1ad8458

View File

@@ -1,4 +1,3 @@
use itertools::Itertools;
use crate::day_solver::DaySolver;
use num::Integer;
@@ -14,21 +13,21 @@ impl Day9 {
}
}
fn print_layout(disk_blocks: &Vec<Block>) {
for block in disk_blocks {
match block {
Block::File(size, idx) => {
for _ in 0..*size {
print!("{}", idx);
}
}
Block::Empty(size) => {
print!("{}", ".".repeat(*size));
}
}
}
println!();
}
// fn print_layout(disk_blocks: &Vec<Block>) {
// for block in disk_blocks {
// match block {
// Block::File(size, idx) => {
// for _ in 0..*size {
// print!("{}", idx);
// }
// }
// Block::Empty(size) => {
// print!("{}", ".".repeat(*size));
// }
// }
// }
// println!();
// }
}
impl DaySolver for Day9 {
@@ -74,45 +73,10 @@ impl DaySolver for Day9 {
res.to_string()
}
//
// fn solve_part2(&mut self) -> String {
// // Maybe I can code it using a vec of the actual disk?
// let mut disk: Vec<Option<usize>> = Vec::with_capacity(self.disk_map.len() * 8);
//
// for (i, b) in self.disk_map.iter().enumerate() {
// if i.is_even() {
// for _ in 0..*b {
// disk.push(Some(i / 2))
// }
// } else {
// for _ in 0..*b {
// disk.push(None)
// }
// }
// }
//
//
// let mut disk_blocks: Vec<Block> = Vec::new();
// for (i, b) in self.disk_map.iter().enumerate() {
// if i.is_even() {
// disk_blocks.push(Block::File(b.to_owned().into(), i / 2))
// } else if b > &0 {
// disk_blocks.push(Block::Empty(b.to_owned().into()))
// }
// }
//
// for block in disk_blocks.iter().rev() {
// if let Block::File(size, idx) = block {
//
// }
// }
//
// "0".to_string()
// }
fn solve_part2(&mut self) -> String {
// Maybe we build the disk
// Somewhat nicer data structure
let mut disk_blocks: Vec<Block> = Vec::new();
for (i, b) in self.disk_map.iter().enumerate() {
if i.is_even() {
@@ -122,7 +86,7 @@ impl DaySolver for Day9 {
}
}
Self::print_layout(&disk_blocks);
// Self::print_layout(&disk_blocks);
let original_disk = disk_blocks.clone();
for block in original_disk.iter().rev() {
@@ -138,24 +102,12 @@ impl DaySolver for Day9 {
match b {
Block::Empty(empty_size) => {
let original_index = disk_blocks.iter().position(|b| b == block).unwrap();
// Never move blocks to the right
if can_move_to_idx >= original_index { continue; }
// We need ot merge
let block_before = disk_blocks.get(original_index - 1).map(|b| b.to_owned());
let block_after = disk_blocks.get(original_index + 1).map(|b| b.to_owned());
// let mut new_empty_space = size.to_owned();
// if let Some(Block::Empty(empty_before)) = block_before {
// new_empty_space += empty_before.to_owned();
// }
// if let Some(Block::Empty(empty_after)) = block_after {
// new_empty_space += empty_after.to_owned();
// }
// We replace the original block by an empty one
// Note that, because we never move blocks to the right, it doesn't matter that the empty space around the block we're moving is inaccurate.
disk_blocks[original_index] = Block::Empty(size.to_owned());
// if let Some(Block::Empty(_)) = block_after {
// disk_blocks.remove(original_index + 1);
// }
// if let Some(Block::Empty(_)) = block_before {
// disk_blocks.remove(original_index - 1);
// }
disk_blocks[can_move_to_idx] = Block::File(size.to_owned(), idx.to_owned());
if &empty_size > size {
// Add additional empty space
@@ -168,7 +120,7 @@ impl DaySolver for Day9 {
}
}
Self::print_layout(&disk_blocks);
// Self::print_layout(&disk_blocks);
let mut checksum = 0usize;
let mut cur_disk_idx = 0usize;
for block in disk_blocks {