This commit is contained in:
JonOfUs 2024-12-11 15:51:07 +01:00
parent 0f75dd0dff
commit eaa9952820
3 changed files with 52 additions and 3 deletions

1
res/11/example Normal file
View file

@ -0,0 +1 @@
125 17

1
res/11/input Normal file
View file

@ -0,0 +1 @@
814 1183689 0 1 766231 4091 93836 46

View file

@ -1,4 +1,4 @@
use std::fs; use std::{collections::HashMap, fs};
pub fn solve() { pub fn solve() {
let path = "res/11/input"; let path = "res/11/input";
@ -6,11 +6,58 @@ pub fn solve() {
#[allow(unused)] #[allow(unused)]
let contents = fs::read_to_string(path).expect("Something went wrong reading the file"); let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
let result: usize = 0; let mut map: HashMap<usize, usize> = HashMap::new();
contents
.trim()
.split(' ')
.map(|n| n.parse::<usize>().unwrap())
.for_each(|n| {
map.insert(n, 1);
});
for _ in 0..25 {
map = blink(map);
}
let result: usize = map.iter().map(|(_, v)| v).sum();
println!("Result 1: {}", result); println!("Result 1: {}", result);
let result: usize = 0; for _ in 0..50 {
map = blink(map);
}
let result: usize = map.iter().map(|(_, v)| v).sum();
println!("Result 2: {}", result); println!("Result 2: {}", result);
} }
fn blink(map: HashMap<usize, usize>) -> HashMap<usize, usize> {
let mut new_map: HashMap<usize, usize> = HashMap::new();
for (k, v) in map.iter() {
if *k == 0 {
*new_map.entry(1).or_insert(0) += *v;
} else if k.to_string().len() % 2 == 0 {
// split num into two halves
let n1 = k
.to_string()
.chars()
.take(k.to_string().len() / 2)
.collect::<String>()
.parse::<usize>()
.unwrap();
let n2 = k
.to_string()
.chars()
.skip(k.to_string().len() / 2)
.collect::<String>()
.parse::<usize>()
.unwrap();
*new_map.entry(n1).or_insert(0) += *v;
*new_map.entry(n2).or_insert(0) += *v;
} else {
*new_map.entry((*k) * 2024).or_insert(0) += *v;
}
}
new_map
}