heh
-rw-r--r--src/main.rs4
-rw-r--r--src/util.rs27
2 files changed, 19 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 8969ecb..a01f169 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,10 +23,12 @@ enum Element {
}
impl Element {
+ #[inline]
fn galaxy(self) -> bool {
self == Self::Galaxy
}
+ #[inline]
fn space(self) -> bool {
self == Self::Space
}
@@ -88,7 +90,7 @@ impl Map<'_> {
(0..S)
.flat_map(|y| (0..S).map(move |x| (x, y)))
.filter(|&(x, y)| self.at(x, y).galaxy())
- .tuple_combinations()
+ .combine()
.map(|((x1, y1), (x2, y2))| {
let expand = 2;
// let expand = 1000000;
diff --git a/src/util.rs b/src/util.rs
index 0b09313..eef3ce0 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -6,8 +6,8 @@ use std::{
pub mod prelude {
pub use super::{
- gcd, lcm, pa, GreekTools, IntoLines, IterͶ, NumTupleIterTools, Skip, TakeLine,
- TupleIterTools, TupleUtils, UnifiedTupleUtils, Widen, Ͷ, Α, Κ, Λ, Μ,
+ gcd, lcm, pa, GreekTools, IntoCombinations, IntoLines, IterͶ, NumTupleIterTools, Skip,
+ TakeLine, TupleIterTools, TupleUtils, UnifiedTupleUtils, Widen, Ͷ, Α, Κ, Λ, Μ,
};
pub use itertools::izip;
pub use itertools::Itertools;
@@ -484,6 +484,20 @@ impl<'b> TakeLine<'b> for &'b str {
}
}
+pub trait IntoCombinations<T: Copy>: Iterator {
+ /// LEAKY
+ fn combine(self) -> impl Iterator<Item = (T, T)>;
+}
+
+impl<T: Copy + 'static, I: Iterator<Item = T>> IntoCombinations<T> for I {
+ fn combine(self) -> impl Iterator<Item = (T, T)> {
+ let x = Box::leak(self.collect::<Box<[_]>>());
+ x.iter()
+ .enumerate()
+ .flat_map(|(i, &a)| x[i..].iter().map(move |&b| (a, b)))
+ }
+}
+
pub trait Skip {
fn skip(&mut self, n: usize);
}
@@ -501,12 +515,3 @@ impl Skip for &str {
*self = &self[n..];
}
}
-
-#[test]
-fn liney() {
- let mut i = b"xyz\nlmaolol\nhuh".行();
- assert_eq!(i.next(), Some(&b"xyz"[..]));
- assert_eq!(i.next(), Some(&b"lmaolol"[..]));
- assert_eq!(i.next(), Some(&b"huh"[..]));
- assert_eq!(i.next(), None);
-}