2024: day 11: part 1 and 2: rust

This commit is contained in:
Samuel Ortion 2024-12-13 18:34:03 +01:00
parent 2ad18dce23
commit 9347243eee
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "day11"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,74 @@
use std::collections::HashMap;
fn blink(stone: u64) -> Vec<u64> {
let mut new_stones: Vec<u64> = Vec::new();
if stone == 0 {
new_stones.push(1);
} else {
let stone_str = stone.to_string();
if stone_str.len() % 2 == 0 {
let index: usize = stone_str.len() / 2;
let part1 = &stone_str[..index];
let part2 = &stone_str[index..];
let part1: u64 = part1.parse().unwrap();
let part2: u64 = part2.parse().unwrap();
new_stones.push(part1);
new_stones.push(part2);
} else {
new_stones.push(stone * 2024);
}
}
new_stones
}
fn blink_synchronous(stones: &Vec<u64>) -> Vec<u64> {
let mut new_stones: Vec<u64> = Vec::new();
for &stone in stones {
let mut stone_children = blink(stone);
new_stones.append(&mut stone_children);
}
new_stones
}
fn blink_memory(stone: u64, nb_blinks: usize, blinked: &mut HashMap<(u64, usize), usize>) -> usize {
if nb_blinks == 0 {
return 1;
} else {
if let Some(size) = blinked.get(&(stone, nb_blinks)) {
return *size;
} else {
let mut sum = 0;
for other_stone in blink(stone) {
sum += blink_memory(other_stone, nb_blinks - 1, blinked);
}
blinked.insert((stone, nb_blinks), sum);
return sum;
}
}
}
fn main() -> std::io::Result<()> {
let stones: String = std::io::read_to_string(std::io::stdin())?;
let mut stones: Vec<u64> = stones
.split(" ")
.map(|e| e.parse::<u64>())
.filter(|e| e.is_ok())
.map(|e| e.unwrap()).collect();
stones.push(1);
println!("{:?}", stones);
// Part 1.
let mut current_stones: Vec<u64> = stones.clone();
for _ in 0..25 {
current_stones = blink_synchronous(&current_stones);
}
println!("{}", current_stones.len());
// Part 2.
let mut blinked: HashMap<(u64, usize), usize> = HashMap::new();
let mut sum: usize = 0;
for stone in stones {
let size = blink_memory(stone, 75, &mut blinked);
sum += size;
}
println!("{}", sum);
Ok(())
}