feat: day 11
This commit is contained in:
parent
ebae29e209
commit
36ff2f2c35
|
@ -15,6 +15,7 @@ full = [
|
|||
"day8",
|
||||
"day9",
|
||||
"day10",
|
||||
"day11",
|
||||
]
|
||||
default = ["full"]
|
||||
day1 = []
|
||||
|
@ -27,6 +28,7 @@ day7 = []
|
|||
day8 = []
|
||||
day9 = []
|
||||
day10 = []
|
||||
day11 = []
|
||||
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
1
inputs/day11/example.txt
Normal file
1
inputs/day11/example.txt
Normal file
|
@ -0,0 +1 @@
|
|||
125 17
|
79
src/day11.rs
Normal file
79
src/day11.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
#![allow(
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_possible_wrap,
|
||||
clippy::too_many_lines
|
||||
)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
fn blink_v2(stones: HashMap<usize, usize>) -> HashMap<usize, usize> {
|
||||
let mut res: HashMap<usize, usize> = HashMap::new();
|
||||
for (stone, count) in stones {
|
||||
match stone {
|
||||
0 => {
|
||||
res.insert(1, *(res.get(&1).unwrap_or(&0)) + count);
|
||||
}
|
||||
s if s.to_string().len() % 2 == 0 => {
|
||||
let s_string = s.to_string();
|
||||
let split = s_string.split_at(s_string.len() / 2);
|
||||
let left_half = split.0.parse::<usize>().unwrap();
|
||||
res.insert(left_half, *(res.get(&left_half).unwrap_or(&0)) + count);
|
||||
let right_half = split.1.parse::<usize>().unwrap();
|
||||
res.insert(right_half, *(res.get(&right_half).unwrap_or(&0)) + count);
|
||||
}
|
||||
v => {
|
||||
let new_stone = v * 2024;
|
||||
res.insert(new_stone, *(res.get(&new_stone).unwrap_or(&0)) + count);
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
pub fn pt1(input: &str) -> usize {
|
||||
let mut stones: HashMap<usize, usize> = HashMap::new();
|
||||
for stone in input
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse::<usize>().unwrap())
|
||||
{
|
||||
stones.insert(stone, stones.get(&stone).unwrap_or(&0) + 1);
|
||||
}
|
||||
for step in 0..25 {
|
||||
debug!("step {step}");
|
||||
stones = blink_v2(stones);
|
||||
}
|
||||
|
||||
stones.values().sum()
|
||||
}
|
||||
pub fn pt2(input: &str) -> usize {
|
||||
let mut stones: HashMap<usize, usize> = HashMap::new();
|
||||
for stone in input
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse::<usize>().unwrap())
|
||||
{
|
||||
stones.insert(stone, stones.get(&stone).unwrap_or(&0) + 1);
|
||||
}
|
||||
for step in 0..75 {
|
||||
debug!("step {step}");
|
||||
stones = blink_v2(stones);
|
||||
}
|
||||
|
||||
stones.values().sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tracing_test::traced_test;
|
||||
|
||||
use super::pt1;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
#[traced_test]
|
||||
#[test]
|
||||
pub fn test_pt1() {
|
||||
let input =
|
||||
read_to_string("./inputs/day11/example.txt").expect("Missing example.txt for day11");
|
||||
assert_eq!(pt1(&input), 55312)
|
||||
}
|
||||
}
|
|
@ -23,6 +23,8 @@ mod day08;
|
|||
mod day09;
|
||||
#[cfg(feature = "day10")]
|
||||
mod day10;
|
||||
#[cfg(feature = "day11")]
|
||||
mod day11;
|
||||
|
||||
fn main() -> eyre::Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
@ -49,6 +51,8 @@ fn main() -> eyre::Result<()> {
|
|||
solve_day(9, day09::pt1, day09::pt2);
|
||||
#[cfg(feature = "day10")]
|
||||
solve_day(10, day10::pt1, day10::pt2);
|
||||
#[cfg(feature = "day11")]
|
||||
solve_day(11, day11::pt1, day11::pt2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue