add combinations iterator. it has bugs
This commit is contained in:
parent
a0002fdd4e
commit
6a58f37ad7
@ -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<Ix2>, upper: impl Into<Ix2>) -> 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()])
|
||||
@ -62,8 +54,9 @@ impl Ix2 {
|
||||
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 {
|
||||
|
@ -1,27 +1,73 @@
|
||||
pub struct Combinations<I: Iterator> {
|
||||
pub struct Combinations<I, T>
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
{
|
||||
indices: Vec<usize>,
|
||||
k: usize,
|
||||
size: usize,
|
||||
pool: Vec<I::Item>,
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<I: Iterator> Combinations<I> {
|
||||
pub fn new(iter: I, comb: usize) -> Self {
|
||||
impl<I, T> Combinations<I, T>
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
{
|
||||
pub fn new(iter: I, k: usize) -> Self {
|
||||
let pool: Vec<I::Item> = iter.collect();
|
||||
|
||||
Self {
|
||||
indices: Vec::with_capacity(comb),
|
||||
indices: (0..k).collect(),
|
||||
k,
|
||||
size: pool.len(),
|
||||
pool,
|
||||
first: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Iterator> Iterator for Combinations<I> {
|
||||
type Item = I::Item;
|
||||
impl<I, T> Iterator for Combinations<I, T>
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
T: Copy,
|
||||
{
|
||||
type Item = Vec<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
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<T>: Iterator<Item = T> + Sized {
|
||||
fn combinations(self, k: usize) -> Combinations<Self, T> {
|
||||
Combinations::new(self, k)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, T> CombinationsIterator<T> for I where I: Iterator<Item = T> {}
|
||||
|
||||
#[test]
|
||||
fn test_iter() {
|
||||
println!("{:?}", [1, 2, 3].iter().combinations(2).collect::<Vec<_>>());
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::util::parse::ParseExt;
|
||||
|
||||
pub fn part1(input: &str) -> impl std::fmt::Display {
|
||||
let vec: Vec<Vec<u32>> = input.lines().map(|s| s.u32s().collect()).combin;
|
||||
//let vec: Vec<Vec<u32>> = 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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user