Compare commits
No commits in common. "96edd60a7c8d09949ebd932c26be38ab90882ad7" and "6a58f37ad7ab94bda1357b3faf1ebf83dd375d00" have entirely different histories.
96edd60a7c
...
6a58f37ad7
@ -6,7 +6,6 @@ where
|
|||||||
k: usize,
|
k: usize,
|
||||||
size: usize,
|
size: usize,
|
||||||
pool: Vec<I::Item>,
|
pool: Vec<I::Item>,
|
||||||
first: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, T> Combinations<I, T>
|
impl<I, T> Combinations<I, T>
|
||||||
@ -16,20 +15,11 @@ where
|
|||||||
pub fn new(iter: I, k: usize) -> Self {
|
pub fn new(iter: I, k: usize) -> Self {
|
||||||
let pool: Vec<I::Item> = iter.collect();
|
let pool: Vec<I::Item> = iter.collect();
|
||||||
|
|
||||||
if k > pool.len() {
|
|
||||||
panic!(
|
|
||||||
"No combinations possible for len {} and k {}",
|
|
||||||
pool.len(),
|
|
||||||
k
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
indices: (0..k).collect(),
|
indices: (0..k).collect(),
|
||||||
k,
|
k,
|
||||||
size: pool.len(),
|
size: pool.len(),
|
||||||
pool,
|
pool,
|
||||||
first: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,26 +32,30 @@ where
|
|||||||
type Item = Vec<T>;
|
type Item = Vec<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if self.first {
|
let mut out = Vec::with_capacity(self.size);
|
||||||
self.first = false;
|
let mut indices = Vec::with_capacity(self.k);
|
||||||
} else {
|
let mut incremented = false;
|
||||||
let mut i = self.k - 1;
|
|
||||||
|
|
||||||
while self.indices[i] == i + self.size - self.k {
|
for i in &self.indices {
|
||||||
if i > 0 {
|
out.push(self.pool[*i]);
|
||||||
i -= 1;
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.indices[i] += 1;
|
|
||||||
for j in i + 1..self.k {
|
|
||||||
self.indices[j] = self.indices[j - 1] + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(self.indices.iter().map(|i| self.pool[*i]).collect())
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,3 +66,8 @@ pub trait CombinationsIterator<T>: Iterator<Item = T> + Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<I, T> CombinationsIterator<T> for I where I: Iterator<Item = T> {}
|
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,47 +1,13 @@
|
|||||||
use crate::util::parse::ParseExt;
|
use crate::util::parse::ParseExt;
|
||||||
|
|
||||||
fn valid_triangle(v: &[u32; 3]) -> bool {
|
|
||||||
v[0] + v[1] > v[2] && v[1] + v[2] > v[0] && v[0] + v[2] > v[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn part1(input: &str) -> impl std::fmt::Display {
|
pub fn part1(input: &str) -> impl std::fmt::Display {
|
||||||
input
|
//let vec: Vec<Vec<u32>> = input.lines().map(|s| s.u32s().collect());
|
||||||
.lines()
|
|
||||||
.map(|s| {
|
""
|
||||||
s.u32s()
|
|
||||||
.fold(([0u32; 3], 0), |(mut a, i), v| {
|
|
||||||
a[i] = v;
|
|
||||||
(a, i + 1)
|
|
||||||
})
|
|
||||||
.0
|
|
||||||
})
|
|
||||||
.filter(valid_triangle)
|
|
||||||
.count()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &str) -> impl std::fmt::Display {
|
pub fn part2(input: &str) -> impl std::fmt::Display {
|
||||||
fn get3<'a>(lines: &'a mut std::str::Lines<'_>) -> Option<(&'a str, &'a str, &'a str)> {
|
""
|
||||||
Some((lines.next()?, lines.next()?, lines.next()?))
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut lines = input.lines();
|
|
||||||
let mut valid = 0;
|
|
||||||
|
|
||||||
while let Some((l1, l2, l3)) = get3(&mut lines) {
|
|
||||||
let l1: Vec<u32> = l1.u32s().collect();
|
|
||||||
let l2: Vec<u32> = l2.u32s().collect();
|
|
||||||
let l3: Vec<u32> = l3.u32s().collect();
|
|
||||||
if valid_triangle(&[l1[0], l2[0], l3[0]]) {
|
|
||||||
valid += 1;
|
|
||||||
}
|
|
||||||
if valid_triangle(&[l1[1], l2[1], l3[1]]) {
|
|
||||||
valid += 1;
|
|
||||||
}
|
|
||||||
if valid_triangle(&[l1[2], l2[2], l3[2]]) {
|
|
||||||
valid += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
valid
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user