Day 11 part 2

This commit is contained in:
Daniel Flanagan 2024-12-12 09:22:53 -06:00
parent f70f537f36
commit e76498d60c
2 changed files with 34 additions and 27 deletions

View file

@ -7,41 +7,47 @@ fn main() {
} }
fn part1(input: &str) -> usize { fn part1(input: &str) -> usize {
stone_blinks(input, 25) stone_blinks_by_count(input, 25)
} }
fn stone_blinks(input: &str, count: usize) -> usize { fn stone_blink(s: usize) -> Vec<usize> {
let mut stones: Vec<usize> = input if s == 0 {
.trim() return vec![1];
.split(" ")
.map(|s| s.parse().unwrap())
.collect();
let mut next_stones = vec![];
for blink in 0..count {
println!("{blink} {}", stones.len());
for (_i, s) in stones.iter().enumerate() {
// next_stones.push(*s)
if *s == 0 {
next_stones.push(1);
continue;
} }
let ss = s.to_string(); let ss = s.to_string();
if ss.len() % 2 == 0 { if ss.len() % 2 == 0 {
let mid = ss.len() / 2; let mid = ss.len() / 2;
let (s1, s2) = (ss[0..mid].parse().unwrap(), ss[mid..].parse().unwrap()); let (s1, s2) = (ss[0..mid].parse().unwrap(), ss[mid..].parse().unwrap());
next_stones.push(s1); return vec![s1, s2];
next_stones.push(s2);
continue;
} }
next_stones.push(s * 2024) vec![s * 2024]
}
stones = next_stones;
next_stones = vec![];
}
stones.len()
} }
fn part2(input: &str) -> usize {} fn stone_blinks_by_count(input: &str, count: usize) -> usize {
let mut stones: HashMap<usize, usize> = input
.trim()
.split(" ")
.map(|n| n.parse().unwrap())
.fold(HashMap::new(), |mut acc, n| {
*acc.entry(n).or_default() += 1;
acc
});
let mut next_stones: HashMap<usize, usize> = HashMap::new();
for _c in 0..count {
for (n, c) in &stones {
for nn in stone_blink(*n) {
*next_stones.entry(nn).or_default() += c;
}
}
println!("afterblink\n{stones:?}\n{next_stones:?}");
(stones, next_stones) = (next_stones, HashMap::new());
}
stones.into_values().sum()
}
fn part2(input: &str) -> usize {
stone_blinks_by_count(input, 75)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -51,6 +57,6 @@ mod tests {
fn test() { fn test() {
let input = r#"125 17"#; let input = r#"125 17"#;
assert_eq!(part1(input), 55312); assert_eq!(part1(input), 55312);
assert_eq!(part2(input), 0); assert_eq!(part2(input), 65601038650482);
} }
} }

View file

@ -10,6 +10,7 @@ pub use std::{
iter::zip, iter::zip,
num::{ParseFloatError, ParseIntError}, num::{ParseFloatError, ParseIntError},
str::FromStr, str::FromStr,
sync::{Arc, LazyLock, Mutex},
}; };
pub type AnyResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; pub type AnyResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;