From c12a644f0026fd3a82c2954836250b82e9416ef6 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Sun, 8 Dec 2024 23:50:44 -0500 Subject: [PATCH] Minor cleanups --- rs/src/day7.rs | 5 +---- rs/src/day8.rs | 19 ++++++++----------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/rs/src/day7.rs b/rs/src/day7.rs index 0b4b9b1..9b50a4a 100644 --- a/rs/src/day7.rs +++ b/rs/src/day7.rs @@ -53,10 +53,7 @@ pub fn part2(data: &str) -> usize { values.push(x / y); } if x > y { - let mut d = 10; - while d <= y { - d *= 10; - } + let d = 10usize.pow(y.checked_ilog10().unwrap_or(0) + 1); if x % d == y { values.push(x / d); } diff --git a/rs/src/day8.rs b/rs/src/day8.rs index ddd4213..9b4e8c5 100644 --- a/rs/src/day8.rs +++ b/rs/src/day8.rs @@ -1,5 +1,5 @@ use std::collections::BTreeSet; -use std::iter; +use std::iter::{once, successors}; use itertools::Itertools; @@ -34,14 +34,11 @@ where .filter(move |point1| point0 != *point1) .flat_map(|(y1, x1)| { let (dy, dx) = (*y1 as isize - *y0 as isize, *x1 as isize - *x0 as isize); - multiples - .clone() - .map(move |i| { - y1.checked_add_signed(i * dy) - .filter(|y| *y < height) - .zip(x1.checked_add_signed(i * dx).filter(|x| *x < width)) - }) - .while_some() + multiples.clone().map_while(move |i| { + y1.checked_add_signed(i * dy) + .zip(x1.checked_add_signed(i * dx)) + .filter(|(y, x)| *y < height && *x < width) + }) }) }) }) @@ -50,11 +47,11 @@ where } pub fn part1(data: &str) -> usize { - solve(data, iter::once(1)) + solve(data, once(1)) } pub fn part2(data: &str) -> usize { - solve(data, iter::successors(Some(0), |i| Some(i + 1))) + solve(data, successors(Some(0), |i| Some(i + 1))) } #[cfg(test)]