heh
| -rw-r--r-- | src/main.rs | 8 | ||||
| -rw-r--r-- | src/util.rs | 35 |
2 files changed, 33 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index 14052dd..7dea105 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,8 +55,8 @@ pub fn p2(i: &str) -> usize { ] .into_iter() .flatten() - .filter(|&(x, _)| x < 131) - .filter(|&(_, y)| y < 131) + .fl(lt(131)) + .fr(lt(131)) .filter(|&(x, y)| C! { i[y as usize * 132 + x as usize] } != b'#') .map(move |(x, y)| (x as u8, y as u8, g + 1)) { @@ -89,8 +89,8 @@ pub fn p1(i: &str) -> usize { ] .into_iter() .flatten() - .filter(|&(x, _)| x < 131) - .filter(|&(_, y)| y < 131) + .fl(lt(131)) + .fr(lt(131)) .filter(|&(x, y)| i[x.nat() * 132 + y.nat()] != b'#') { let cache_key = is_odd * i.len() + x.nat() * 132 + y.nat(); diff --git a/src/util.rs b/src/util.rs index 3b489a5..f20d7d2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -13,9 +13,9 @@ pub mod prelude { #[allow(unused_imports)] pub(crate) use super::{bits, dang, leek, mat, shucks, C}; pub use super::{ - even, gcd, lcm, pa, Dir, GreekTools, IntoCombinations, IntoLines, IterͶ, NumTupleIterTools, - ParseIter, Printable, Skip, TakeLine, TupleIterTools2, TupleIterTools3, TupleUtils, - UnifiedTupleUtils, UnsoundUtilities, Widen, 読む, 読む::Ext, Ͷ, Α, Κ, Λ, Μ, + even, gcd, gt, lcm, lt, pa, Dir, FilterBy, GreekTools, IntoCombinations, IntoLines, IterͶ, + NumTupleIterTools, ParseIter, Printable, Skip, TakeLine, TupleIterTools2, TupleIterTools3, + TupleUtils, UnifiedTupleUtils, UnsoundUtilities, Widen, 読む, 読む::Ext, Ͷ, Α, Κ, Λ, Μ, }; pub use itertools::izip; pub use itertools::Itertools; @@ -670,6 +670,14 @@ impl Μ for &[u8] { } } +pub fn gt<A: std::cmp::PartialOrd<T>, T>(n: T) -> impl Fn(A) -> bool { + move |a| a > n +} + +pub fn lt<A: std::cmp::PartialOrd<T>, T>(n: T) -> impl Fn(A) -> bool { + move |a| a < n +} + impl Μ for &str { fn μ(self, d: char) -> (Self, Self) { self.split_once(d) @@ -709,6 +717,21 @@ pub trait TupleIterTools2<T, U>: Iterator { fn r(self) -> impl Iterator<Item = U>; } +pub trait FilterBy<T, U>: Iterator { + fn fl(self, f: impl Fn(T) -> bool) -> impl Iterator<Item = (T, U)>; + fn fr(self, f: impl Fn(U) -> bool) -> impl Iterator<Item = (T, U)>; +} + +impl<T: Copy, U: Copy, I: Iterator<Item = (T, U)>> FilterBy<T, U> for I { + fn fl(self, f: impl Fn(T) -> bool) -> impl Iterator<Item = (T, U)> { + self.filter(move |(x, _)| f(*x)) + } + + fn fr(self, f: impl Fn(U) -> bool) -> impl Iterator<Item = (T, U)> { + self.filter(move |(_, x)| f(*x)) + } +} + pub trait NumTupleIterTools { fn πολλαπλασιάζω_και_αθροίζω(&mut self) -> u64; } @@ -719,12 +742,12 @@ impl<I: Iterator<Item = (u64, u64)>> NumTupleIterTools for I { } } -impl<T, U, I: Iterator<Item = (T, U)>> TupleIterTools2<T, U> for I { +impl<'a, T: Copy + 'a, U: Copy + 'a, I: Iterator<Item = &'a (T, U)>> TupleIterTools2<T, U> for I { fn l(self) -> impl Iterator<Item = T> { - self.map(|(x, _)| x) + self.map(|&(x, _)| x) } fn r(self) -> impl Iterator<Item = U> { - self.map(|(_, x)| x) + self.map(|&(_, x)| x) } } |