From 6a58f37ad7ab94bda1357b3faf1ebf83dd375d00 Mon Sep 17 00:00:00 2001 From: fuckwit Date: Thu, 16 Nov 2023 23:12:26 +0100 Subject: [PATCH] add combinations iterator. it has bugs --- input/year2016/day03.txt | 2 +- src/util/index.rs | 19 ++++-------- src/util/iter.rs | 64 ++++++++++++++++++++++++++++++++++------ src/year2016/day03.rs | 4 +-- 4 files changed, 64 insertions(+), 25 deletions(-) diff --git a/input/year2016/day03.txt b/input/year2016/day03.txt index 68a1bab..40378d0 100644 --- a/input/year2016/day03.txt +++ b/input/year2016/day03.txt @@ -1596,4 +1596,4 @@ 188 509 421 850 394 702 68 744 665 - 919 923 873 + 919 923 873 \ No newline at end of file diff --git a/src/util/index.rs b/src/util/index.rs index 736f18f..377fe7d 100644 --- a/src/util/index.rs +++ b/src/util/index.rs @@ -9,12 +9,14 @@ macro_rules! impl_ix { pub type $t = Dim<[Ix; $n]>; impl From<[Ix; $n]> for $t { + #[inline] fn from(value: [Ix; $n]) -> Self { Dim(value) } } #[allow(non_snake_case)] + #[inline] pub fn $t() -> Dim<[Ix; $n]> { Dim([0; $n]) } )+ }; @@ -42,16 +44,6 @@ impl Ix2 { self.0[1] } - pub fn within(&self, lower: impl Into, upper: impl Into) -> bool { - let lower = lower.into(); - let upper = upper.into(); - - self.x() >= lower.x() - && self.x() <= upper.x() - && self.y() >= lower.y() - && self.y() <= upper.y() - } - #[inline] pub fn clockwise(self) -> Self { Dim([-self.y(), self.x()]) @@ -61,11 +53,12 @@ impl Ix2 { pub fn counter_clockwise(self) -> Self { Dim([self.y(), -self.x()]) } - + + #[inline] pub fn manhatten(&self, other: Ix2) -> i64 { - (self.0[0] - other.0[0]).abs() + (self.0[1] - other.0[1]).abs() + (self.x() - other.x()).abs() + (self.y() - other.y()).abs() } - + pub fn linearize(&self, width: usize, height: usize) -> usize { self.y() as usize * height + (width - 1) } diff --git a/src/util/iter.rs b/src/util/iter.rs index 8706af2..99fa977 100644 --- a/src/util/iter.rs +++ b/src/util/iter.rs @@ -1,27 +1,73 @@ -pub struct Combinations { +pub struct Combinations +where + I: Iterator, +{ indices: Vec, + k: usize, size: usize, pool: Vec, - first: bool, } -impl Combinations { - pub fn new(iter: I, comb: usize) -> Self { +impl Combinations +where + I: Iterator, +{ + pub fn new(iter: I, k: usize) -> Self { let pool: Vec = iter.collect(); Self { - indices: Vec::with_capacity(comb), + indices: (0..k).collect(), + k, size: pool.len(), pool, - first: true, } } } -impl Iterator for Combinations { - type Item = I::Item; +impl Iterator for Combinations +where + I: Iterator, + T: Copy, +{ + type Item = Vec; fn next(&mut self) -> Option { - todo!() + let mut out = Vec::with_capacity(self.size); + let mut indices = Vec::with_capacity(self.k); + let mut incremented = false; + + for i in &self.indices { + out.push(self.pool[*i]); + } + + self.indices.reverse(); + let mut iter = self.indices.iter().enumerate(); + while let Some((count, idx)) = iter.next() { + if incremented || *idx == self.size - (count + 1) { + indices.push(*idx); + continue; + } + indices.push(idx + 1); + incremented = true; + } + if !incremented { + return None; + } + indices.reverse(); + self.indices = indices; + Some(out) } } + +pub trait CombinationsIterator: Iterator + Sized { + fn combinations(self, k: usize) -> Combinations { + Combinations::new(self, k) + } +} + +impl CombinationsIterator for I where I: Iterator {} + +#[test] +fn test_iter() { + println!("{:?}", [1, 2, 3].iter().combinations(2).collect::>()); +} diff --git a/src/year2016/day03.rs b/src/year2016/day03.rs index 4551605..6cb45dc 100644 --- a/src/year2016/day03.rs +++ b/src/year2016/day03.rs @@ -1,7 +1,7 @@ use crate::util::parse::ParseExt; pub fn part1(input: &str) -> impl std::fmt::Display { - let vec: Vec> = input.lines().map(|s| s.u32s().collect()).combin; + //let vec: Vec> = input.lines().map(|s| s.u32s().collect()); "" } @@ -17,5 +17,5 @@ fn test_part1() { #[test] fn test_part2() { - assert_eq!("", part2("").to_string()) + assert_eq!("", part2("").to_string()); }