Day 11 part 2
This commit is contained in:
parent
f70f537f36
commit
e76498d60c
2 changed files with 34 additions and 27 deletions
|
@ -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 {
|
||||||
|
return vec![1];
|
||||||
|
}
|
||||||
|
let ss = s.to_string();
|
||||||
|
if ss.len() % 2 == 0 {
|
||||||
|
let mid = ss.len() / 2;
|
||||||
|
let (s1, s2) = (ss[0..mid].parse().unwrap(), ss[mid..].parse().unwrap());
|
||||||
|
return vec![s1, s2];
|
||||||
|
}
|
||||||
|
vec![s * 2024]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stone_blinks_by_count(input: &str, count: usize) -> usize {
|
||||||
|
let mut stones: HashMap<usize, usize> = input
|
||||||
.trim()
|
.trim()
|
||||||
.split(" ")
|
.split(" ")
|
||||||
.map(|s| s.parse().unwrap())
|
.map(|n| n.parse().unwrap())
|
||||||
.collect();
|
.fold(HashMap::new(), |mut acc, n| {
|
||||||
let mut next_stones = vec![];
|
*acc.entry(n).or_default() += 1;
|
||||||
for blink in 0..count {
|
acc
|
||||||
println!("{blink} {}", stones.len());
|
});
|
||||||
for (_i, s) in stones.iter().enumerate() {
|
let mut next_stones: HashMap<usize, usize> = HashMap::new();
|
||||||
// next_stones.push(*s)
|
for _c in 0..count {
|
||||||
if *s == 0 {
|
for (n, c) in &stones {
|
||||||
next_stones.push(1);
|
for nn in stone_blink(*n) {
|
||||||
continue;
|
*next_stones.entry(nn).or_default() += c;
|
||||||
}
|
}
|
||||||
let ss = s.to_string();
|
|
||||||
if ss.len() % 2 == 0 {
|
|
||||||
let mid = ss.len() / 2;
|
|
||||||
let (s1, s2) = (ss[0..mid].parse().unwrap(), ss[mid..].parse().unwrap());
|
|
||||||
next_stones.push(s1);
|
|
||||||
next_stones.push(s2);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
next_stones.push(s * 2024)
|
|
||||||
}
|
}
|
||||||
stones = next_stones;
|
println!("afterblink\n{stones:?}\n{next_stones:?}");
|
||||||
next_stones = vec![];
|
(stones, next_stones) = (next_stones, HashMap::new());
|
||||||
}
|
}
|
||||||
stones.len()
|
stones.into_values().sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: &str) -> usize {}
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>>;
|
||||||
|
|
Loading…
Reference in a new issue