1
0

day 11 a bit faster

This commit is contained in:
2024-12-11 21:12:37 +01:00
parent 3cab145b01
commit 3f94690477

View File

@@ -14,25 +14,23 @@ fn part1(input: &str) -> RiddleResult {
} }
fn solve(input: &str, blinks: i32) -> usize { fn solve(input: &str, blinks: i32) -> usize {
let mut stones: HashMap<String, usize> = input.split(" ").map(|v| (v.to_string(), 1)).collect(); let mut stones: HashMap<usize, usize> =
input.split(" ").map(|v| (v.parse().unwrap(), 1)).collect();
let mut next = HashMap::new(); let mut next = HashMap::new();
for _ in 0..blinks { for _ in 0..blinks {
for (k, v) in stones { for (k, v) in stones {
if k.as_str() == "0" { if k == 0 {
next.entry("1".to_string()) next.entry(1).and_modify(|count| *count += v).or_insert(v);
.and_modify(|count| *count += v) } else if (k.ilog10() + 1) % 2 == 0 {
.or_insert(v); let l = (k.ilog10() + 1) / 2;
} else if k.len() % 2 == 0 { let z: usize = 10usize.pow(l as u32);
let (a, b) = k.split_at(k.len() / 2); let a = k / z;
next.entry(a.to_string()) let b = k % z;
.and_modify(|count| *count += v) next.entry(a).and_modify(|count| *count += v).or_insert(v);
.or_insert(v); next.entry(b).and_modify(|count| *count += v).or_insert(v);
let x = b.parse::<usize>().unwrap().to_string();
next.entry(x).and_modify(|count| *count += v).or_insert(v);
} else { } else {
let d: usize = k.parse().unwrap(); let k_new = k * 2024;
let k_new = d * 2024; next.entry(k_new)
next.entry(k_new.to_string())
.and_modify(|count| *count += v) .and_modify(|count| *count += v)
.or_insert(v); .or_insert(v);
} }
@@ -49,17 +47,10 @@ fn part2(input: &str) -> RiddleResult {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{part1, part2}; use super::part1;
const TEST_INPUT: &str = r"";
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(part1("125 17"), 55312); assert_eq!(part1("125 17"), 55312);
} }
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 0);
}
} }