From 2a96c16b4b63973d0f2a0132615690fad2c8ba10 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Tue, 10 Dec 2024 21:59:14 -0500 Subject: [PATCH] Complete day 9 part 1 --- src/day09.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 +-- 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/day09.rs diff --git a/src/day09.rs b/src/day09.rs new file mode 100644 index 0000000..e80700f --- /dev/null +++ b/src/day09.rs @@ -0,0 +1,124 @@ +fn input() -> &'static str { + include_str!("../input/day09.txt") +} + +type DiskMap = Option; + +fn parse(input: &str) -> Vec { + input + .trim() + .chars() + .zip(0..) + .flat_map(|(c, i)| { + let n: u64 = c.to_string().parse().unwrap(); + if i % 2 == 0 { + vec![Some(i / 2); n.try_into().unwrap()] + } else { + vec![None; n.try_into().unwrap()] + } + }) + .collect() +} + +fn fragment(disk: &mut Vec) { + let mut ptr = 0; + loop { + while ptr < disk.len() && disk[ptr].is_some() { + ptr += 1; + } + if ptr < disk.len() { + disk[ptr] = disk.pop().unwrap(); + } else { + break; + } + } +} + +fn checksum(disk: &[DiskMap]) -> u64 { + disk.iter() + .zip(0..) + .map(|(n, i)| match n { + Some(n) => n * i, + None => 0, + }) + .sum() +} + +pub fn part1() { + let mut map = parse(input()); + fragment(&mut map); + let n = checksum(&map); + println!("Day 9 Part 1: {}", n); +} + +pub fn part2() { + let n = 0; + println!("Day 9 Part 2: {}", n); +} + +#[cfg(test)] +mod test { + use super::*; + + const INPUT_STR: &str = concat!("2333133121414131402\n"); + + #[rustfmt::skip] + #[test] + fn test_parse() { + assert_eq!( + parse(INPUT_STR), + vec![ + Some(0), Some(0), + None, None, None, + Some(1), Some(1), Some(1), + None, None, None, + Some(2), + None, None, None, + Some(3), Some(3), Some(3), + None, + Some(4), Some(4), + None, + Some(5), Some(5), Some(5), Some(5), + None, + Some(6), Some(6), Some(6), Some(6), + None, + Some(7), Some(7), Some(7), + None, + Some(8), Some(8), Some(8), Some(8), + Some(9), Some(9) + ], + ) + } + + #[rustfmt::skip] + #[test] + fn test_fragment() { + let mut map = parse(INPUT_STR); + fragment(&mut map); + assert_eq!( + map, + vec![ + Some(0), Some(0), + Some(9), Some(9), + Some(8), + Some(1), Some(1), Some(1), + Some(8), Some(8), Some(8), + Some(2), + Some(7), Some(7), Some(7), + Some(3), Some(3), Some(3), + Some(6), + Some(4), Some(4), + Some(6), + Some(5), Some(5), Some(5), Some(5), + Some(6), Some(6), + ], + ) + } + + #[test] + fn test_checksum() { + let mut map = parse(INPUT_STR); + fragment(&mut map); + assert_eq!(checksum(&map), 1928) + } +} diff --git a/src/main.rs b/src/main.rs index 879fa54..0a184da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ pub mod day05; pub mod day06; pub mod day07; pub mod day08; -// pub mod day09; +pub mod day09; // pub mod day10; // pub mod day11; // pub mod day12; @@ -26,7 +26,7 @@ pub mod day08; type Part = fn(); -const DAYS: [(Part, Part); 8] = [ +const DAYS: [(Part, Part); 9] = [ (day01::part1 as fn(), day01::part2 as fn()), (day02::part1 as fn(), day02::part2 as fn()), (day03::part1 as fn(), day03::part2 as fn()), @@ -35,7 +35,7 @@ const DAYS: [(Part, Part); 8] = [ (day06::part1 as fn(), day06::part2 as fn()), (day07::part1 as fn(), day07::part2 as fn()), (day08::part1 as fn(), day08::part2 as fn()), - // (day09::part1 as fn(), day09::part2 as fn()), + (day09::part1 as fn(), day09::part2 as fn()), // (day10::part1 as fn(), day10::part2 as fn()), // (day11::part1 as fn(), day11::part2 as fn()), // (day12::part1 as fn(), day12::part2 as fn()), -- 2.38.5