diff --git a/src/day9.rs b/src/day9.rs index a090aff..01c712f 100644 --- a/src/day9.rs +++ b/src/day9.rs @@ -1,6 +1,6 @@ use aoc_runner_derive::{aoc, aoc_generator}; -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum Disk { File { id: usize, blocks: usize }, Free { blocks: usize }, @@ -28,16 +28,69 @@ pub fn input_generator(input: &str) -> Vec { #[aoc(day9, part1)] pub fn part1(input: &[Disk]) -> usize { - dbg!(input); - 0 -} + for i in input { + match i { + Disk::File { id, blocks } => { + print!("{}", id.to_string().repeat(*blocks)); + } + Disk::Free { blocks } => { + print!("{}", ".".repeat(*blocks)); + } + } + } + println!(); + + let mut i = 0; + let mut input = input.to_vec(); + let mut results: Vec = Vec::new(); + + loop { + if i > input.len() { + break; + } + + match input[i] { + Disk::File { id, blocks } => results.extend(std::iter::repeat(id).take(blocks)), + Disk::Free { + blocks: free_blocks, + } => loop { + if let Some(last) = input.pop() { + match last { + Disk::Free { blocks } => continue, + Disk::File { id: _, blocks } => { + if blocks == *free_blocks { + results.push(last); + } + } + } + } else { + break; + } + }, + } + } + + for i in results { + match i { + Disk::File { id, blocks } => { + print!("{}", id.to_string().repeat(blocks)); + } + Disk::Free { blocks } => { + print!("{}", ".".repeat(blocks)); + } + } + } + println!(); -#[aoc(day9, part2)] -pub fn part2(input: &[Disk]) -> usize { - dbg!(input); 0 } +// #[aoc(day9, part2)] +// pub fn part2(input: &[Disk]) -> usize { +// // dbg!(input); +// 0 +// } + #[cfg(test)] mod tests { use super::*; @@ -49,8 +102,8 @@ mod tests { assert_eq!(1, part1(&input_generator(INPUT))) } - #[test] - fn test_part2() { - assert_eq!(1, part2(&input_generator(INPUT))) - } + // #[test] + // fn test_part2() { + // assert_eq!(1, part2(&input_generator(INPUT))) + // } }