From 7e91b9be1d04ddf25492990e9924fe361621ffc0 Mon Sep 17 00:00:00 2001 From: Himangshu Saikia Date: Sat, 28 Dec 2024 09:41:11 +0100 Subject: [PATCH] 2024_03 refactor --- src/bin/2024_03/main.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/bin/2024_03/main.rs b/src/bin/2024_03/main.rs index d80a1d8..0957e51 100644 --- a/src/bin/2024_03/main.rs +++ b/src/bin/2024_03/main.rs @@ -23,26 +23,16 @@ fn solve(input: &str) -> usize { if PART == 1 { compute(input) } else { - let do_dont_indices = input - .match_indices("do()") + let extreme_checkpoints = [(0, "do()"), (input.len(), "don't()")]; + let checkpoints: Vec<(usize, &str)> = extreme_checkpoints + .into_iter() + .chain(input.match_indices("do()")) .chain(input.match_indices("don't()")) .sorted_by_key(|(idx, _)| *idx) - .map(|(idx, do_or_dont)| { - if do_or_dont == "do()" { - (idx, true) - } else { - (idx, false) - } - }); - - let mut checkpoints = vec![(0, true)]; - for idx in do_dont_indices { - checkpoints.push(idx); - } - checkpoints.push((input.len(), false)); + .collect(); checkpoints.windows(2).fold(0, |mut acc, interval| { - if interval[0].1 { + if interval[0].1 == "do()" { acc += compute(&input[interval[0].0..interval[1].0]); } acc