From c19088f3cb20a928c9dc39cf64bdf00a8b160f1d Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 21 Dec 2021 21:57:53 +0100 Subject: [PATCH] Optimize 21/1 --- 2021/src/bin/day21.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/2021/src/bin/day21.rs b/2021/src/bin/day21.rs index dc4877f..ab2da28 100644 --- a/2021/src/bin/day21.rs +++ b/2021/src/bin/day21.rs @@ -10,18 +10,15 @@ fn part1((p1, p2): Parsed) -> usize { .cycle() .skip(1) .step_by(3) - .enumerate() - .scan([(p1, 0), (p2, 0)], |mut scores, (round, die)| { + .zip(1..) + .scan([(p2, 0), (p1, 0)], |mut scores, (die, round)| { let mut points = scores[round & 1].0 + die * 3; - // can’t use mod 10 because we actually want to keep the 10 - while points > 10 { - points -= 10; - } + points -= (points - 1) / 10 * 10; scores[round & 1].0 = points; scores[round & 1].1 += points; - Some((round + 1, *scores)) + Some((round, (scores[0].1, scores[1].1))) }) - .find_map(|(r, [(_, s1), (_, s2)])| (s1 >= 1000 || s2 >= 1000).then(|| r * 3 * (s1.min(s2) as usize))) + .find_map(|(r, (s1, s2))| (s1 >= 1000 || s2 >= 1000).then(|| r * 3 * (s1.min(s2) as usize))) .unwrap() }