From b172059d119462c8eaececc339e1189847b94875 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 11 Dec 2024 19:36:25 +0100 Subject: [PATCH] A bit quicker and more readable without recursion --- src/day11.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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)] -- 2.45.2