diff --git a/res/11/example b/res/11/example new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/res/11/example @@ -0,0 +1 @@ +125 17 diff --git a/res/11/input b/res/11/input new file mode 100644 index 0000000..557c831 --- /dev/null +++ b/res/11/input @@ -0,0 +1 @@ +814 1183689 0 1 766231 4091 93836 46 diff --git a/src/days/d11.rs b/src/days/d11.rs index 3e321f8..56793d4 100644 --- a/src/days/d11.rs +++ b/src/days/d11.rs @@ -1,4 +1,4 @@ -use std::fs; +use std::{collections::HashMap, fs}; pub fn solve() { let path = "res/11/input"; @@ -6,11 +6,58 @@ pub fn solve() { #[allow(unused)] let contents = fs::read_to_string(path).expect("Something went wrong reading the file"); - let result: usize = 0; + let mut map: HashMap = HashMap::new(); + contents + .trim() + .split(' ') + .map(|n| n.parse::().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); - let result: usize = 0; + for _ in 0..50 { + map = blink(map); + } + + let result: usize = map.iter().map(|(_, v)| v).sum(); println!("Result 2: {}", result); } + +fn blink(map: HashMap) -> HashMap { + let mut new_map: HashMap = 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::() + .parse::() + .unwrap(); + let n2 = k + .to_string() + .chars() + .skip(k.to_string().len() / 2) + .collect::() + .parse::() + .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 +}