add combinations iterator. it has bugs
This commit is contained in:
parent
a0002fdd4e
commit
6a58f37ad7
@ -1596,4 +1596,4 @@
|
|||||||
188 509 421
|
188 509 421
|
||||||
850 394 702
|
850 394 702
|
||||||
68 744 665
|
68 744 665
|
||||||
919 923 873
|
919 923 873
|
@ -9,12 +9,14 @@ macro_rules! impl_ix {
|
|||||||
pub type $t = Dim<[Ix; $n]>;
|
pub type $t = Dim<[Ix; $n]>;
|
||||||
|
|
||||||
impl From<[Ix; $n]> for $t {
|
impl From<[Ix; $n]> for $t {
|
||||||
|
#[inline]
|
||||||
fn from(value: [Ix; $n]) -> Self {
|
fn from(value: [Ix; $n]) -> Self {
|
||||||
Dim(value)
|
Dim(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
|
#[inline]
|
||||||
pub fn $t() -> Dim<[Ix; $n]> { Dim([0; $n]) }
|
pub fn $t() -> Dim<[Ix; $n]> { Dim([0; $n]) }
|
||||||
)+
|
)+
|
||||||
};
|
};
|
||||||
@ -42,16 +44,6 @@ impl Ix2 {
|
|||||||
self.0[1]
|
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]
|
#[inline]
|
||||||
pub fn clockwise(self) -> Self {
|
pub fn clockwise(self) -> Self {
|
||||||
Dim([-self.y(), self.x()])
|
Dim([-self.y(), self.x()])
|
||||||
@ -61,11 +53,12 @@ impl Ix2 {
|
|||||||
pub fn counter_clockwise(self) -> Self {
|
pub fn counter_clockwise(self) -> Self {
|
||||||
Dim([self.y(), -self.x()])
|
Dim([self.y(), -self.x()])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn manhatten(&self, other: Ix2) -> i64 {
|
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 {
|
pub fn linearize(&self, width: usize, height: usize) -> usize {
|
||||||
self.y() as usize * height + (width - 1)
|
self.y() as usize * height + (width - 1)
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,73 @@
|
|||||||
pub struct Combinations<I: Iterator> {
|
pub struct Combinations<I, T>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = T>,
|
||||||
|
{
|
||||||
indices: Vec<usize>,
|
indices: Vec<usize>,
|
||||||
|
k: usize,
|
||||||
size: usize,
|
size: usize,
|
||||||
pool: Vec<I::Item>,
|
pool: Vec<I::Item>,
|
||||||
first: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: Iterator> Combinations<I> {
|
impl<I, T> Combinations<I, T>
|
||||||
pub fn new(iter: I, comb: usize) -> Self {
|
where
|
||||||
|
I: Iterator<Item = T>,
|
||||||
|
{
|
||||||
|
pub fn new(iter: I, k: usize) -> Self {
|
||||||
let pool: Vec<I::Item> = iter.collect();
|
let pool: Vec<I::Item> = iter.collect();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
indices: Vec::with_capacity(comb),
|
indices: (0..k).collect(),
|
||||||
|
k,
|
||||||
size: pool.len(),
|
size: pool.len(),
|
||||||
pool,
|
pool,
|
||||||
first: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: Iterator> Iterator for Combinations<I> {
|
impl<I, T> Iterator for Combinations<I, T>
|
||||||
type Item = I::Item;
|
where
|
||||||
|
I: Iterator<Item = T>,
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
type Item = Vec<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
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;
|
use crate::util::parse::ParseExt;
|
||||||
|
|
||||||
pub fn part1(input: &str) -> impl std::fmt::Display {
|
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]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2() {
|
||||||
assert_eq!("", part2("").to_string())
|
assert_eq!("", part2("").to_string());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user