From: Greg Burri Date: Wed, 11 Dec 2024 18:36:25 +0000 (+0100) Subject: A bit quicker and more readable without recursion X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=b172059d119462c8eaececc339e1189847b94875;p=advent_of_code_2024.git A bit quicker and more readable without recursion --- diff --git a/src/day11.rs b/src/day11.rs index be2f8f8..5567a16 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -16,10 +16,8 @@ fn add_or_set(map: &mut Stones, k: u64, n: u64) { map.entry(k).and_modify(|v| *v += n).or_insert(n); } -pub fn blink(stones: Stones, i: u32) -> Stones { - if i == 0 { - stones - } else { +pub fn blink(mut stones: Stones, i: u32) -> Stones { + for _ in 0..i { let mut next_stones = Stones::default(); for (stone, n) in stones { if stone == 0 { @@ -37,9 +35,9 @@ pub fn blink(stones: Stones, i: u32) -> Stones { } } } - - blink(next_stones, i - 1) + stones = next_stones; } + stones } #[cfg(test)]