From 49ed857a18ef0fd6c65b5bed0d559616af0913e5 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Sun, 15 Dec 2024 22:38:44 +0100 Subject: [PATCH] day_11: part 1 done --- README.md | 2 +- assets/day_11_stone_arrangement_input | 1 + src/advent_of_code/mod.rs | 18 ++++++--- src/advent_of_code/types.rs | 5 +++ src/advent_of_code/utils.rs | 12 ++++++ src/advent_of_code/wed_11.rs | 53 +++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 assets/day_11_stone_arrangement_input create mode 100644 src/advent_of_code/wed_11.rs diff --git a/README.md b/README.md index c924392..341adf8 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ https://adventofcode.com/2024 | 08 | Resonant Collinearity | :pushpin: | | Matrix exploration | | 09 | Disk fragmenter | :pushpin: | | Array indexing | | 10 | Hoof It | :pushpin: | :pushpin: | Path finding | -| 11 | | | | | +| 11 | Plutonian Pebbles | :pushpin: | | Array and digit manipulation | | 12 | | | | | | 13 | | | | | | 14 | | | | | diff --git a/assets/day_11_stone_arrangement_input b/assets/day_11_stone_arrangement_input new file mode 100644 index 0000000..4c7941d --- /dev/null +++ b/assets/day_11_stone_arrangement_input @@ -0,0 +1 @@ +2 77706 5847 9258441 0 741 883933 12 diff --git a/src/advent_of_code/mod.rs b/src/advent_of_code/mod.rs index 86a0b69..acb77f9 100644 --- a/src/advent_of_code/mod.rs +++ b/src/advent_of_code/mod.rs @@ -7,9 +7,10 @@ // mod sat_07; // mod sun_08; // mod mon_09; -mod tue_10; +// mod tue_10; mod types; mod utils; +mod wed_11; use types::*; @@ -78,8 +79,15 @@ pub fn historian_hysteria() { // println!("The disk checksum is {}", disk_checksum); // Tuesday 10 - let (trailhead_score, full_trailhead_score) = - tue_10::hoof_it("./assets/day_10_trail_map_input"); - println!("The trail head sum score is {}", trailhead_score); - println!("The full trail head sum score is {}", full_trailhead_score); + // let (trailhead_score, full_trailhead_score) = + // tue_10::hoof_it("./assets/day_10_trail_map_input"); + // println!("The trail head sum score is {}", trailhead_score); + // println!("The full trail head sum score is {}", full_trailhead_score); + let blink_count: usize = 25; + let stone_count = + wed_11::plutonian_pebbles("./assets/day_11_stone_arrangement_input", blink_count); + println!( + "The total of stones after {} is {}", + blink_count, stone_count + ); } diff --git a/src/advent_of_code/types.rs b/src/advent_of_code/types.rs index 05b880a..f0c0bfc 100644 --- a/src/advent_of_code/types.rs +++ b/src/advent_of_code/types.rs @@ -81,3 +81,8 @@ pub type DiskChecksum = usize; pub type TrailScore = usize; pub type TrailPosition = usize; pub type TrailMap = Vec>; + +// Plutonian Pebbles +pub type StoneCount = usize; +pub type Stone = usize; +pub type StoneList = Vec; diff --git a/src/advent_of_code/utils.rs b/src/advent_of_code/utils.rs index 18ee48b..2d3d747 100644 --- a/src/advent_of_code/utils.rs +++ b/src/advent_of_code/utils.rs @@ -206,6 +206,18 @@ pub fn read_trail_map_input(input: &str) -> TrailMap { trail_map } +pub fn read_stone_arrangement(input: &str) -> StoneList { + let mut stone_list: StoneList = vec![]; + + for line in read_to_string(input).unwrap().lines() { + for number in line.split(" ") { + stone_list.push(number.parse::().unwrap()); + } + } + + stone_list +} + pub fn calc_distance(num_1: T, num_2: T) -> T where T: PartialOrd + Sub, diff --git a/src/advent_of_code/wed_11.rs b/src/advent_of_code/wed_11.rs new file mode 100644 index 0000000..06d30cd --- /dev/null +++ b/src/advent_of_code/wed_11.rs @@ -0,0 +1,53 @@ +use super::*; + +pub fn plutonian_pebbles(input: &str, blink_count: usize) -> StoneCount { + let stone_list = utils::read_stone_arrangement(input); + + let stone_list = apply_rules(stone_list, blink_count); + + stone_list.len() +} + +fn apply_rules(stone_list: StoneList, blink_count: usize) -> StoneList { + let mut new_stone_list = stone_list.clone(); + + for _ in 0..blink_count { + new_stone_list = blink(new_stone_list); + } + + new_stone_list +} + +fn blink(stone_list: StoneList) -> StoneList { + let mut new_stone_list: StoneList = vec![]; + + const STONE_MULTIPLIER: usize = 2024; + for stone in stone_list { + match stone { + 0 => { + new_stone_list.push(1); + } + stone if stone.to_string().len() % 2 == 0 => { + let (left, right) = split_num(stone); + new_stone_list.push(left); + new_stone_list.push(right); + } + _ => { + new_stone_list.push(stone * STONE_MULTIPLIER); + } + } + } + + new_stone_list +} + +fn split_num(num: Stone) -> (Stone, Stone) { + let split_index = num.to_string().len() / 2; + let binding = num.to_string(); + let (first, second) = binding.split_at(split_index); + + ( + first.parse::().unwrap(), + second.parse::().unwrap(), + ) +}