[Rust/2023/11] Add solution
This commit is contained in:
parent
a319eaf488
commit
6d3cb9453c
7 changed files with 77 additions and 24 deletions
File diff suppressed because one or more lines are too long
51
Rust/2023/11.rs
Normal file
51
Rust/2023/11.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
#![feature(test)]
|
||||
|
||||
use aoc::iter_ext::IterExt;
|
||||
use itertools::Itertools;
|
||||
|
||||
type Input = Vec<Vec<bool>>;
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| line.bytes().map(|b| b == b'#').collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn solve<const N: usize>(input: &Input) -> usize {
|
||||
let empty_rows = input.iter().map(|row| row.iter().all(|x| !x)).collect_vec();
|
||||
let empty_cols = input
|
||||
.iter()
|
||||
.transpose()
|
||||
.map(|row| row.iter().all(|&x| !x))
|
||||
.collect_vec();
|
||||
|
||||
input
|
||||
.iter()
|
||||
.zip(empty_rows)
|
||||
.scan(0, |i, (row, empty)| {
|
||||
*i += if empty { N } else { 1 };
|
||||
let i = *i;
|
||||
Some(row.iter().zip(&empty_cols).scan(0, move |j, (&x, &empty)| {
|
||||
*j += if empty { N } else { 1 };
|
||||
Some((i, *j, x))
|
||||
}))
|
||||
})
|
||||
.flatten()
|
||||
.filter_map(|(i, j, x)| x.then_some((i, j)))
|
||||
.collect_vec()
|
||||
.into_iter()
|
||||
.tuple_combinations()
|
||||
.map(|(a, b)| a.0.abs_diff(b.0) + a.1.abs_diff(b.1))
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> usize {
|
||||
solve::<2>(input)
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> usize {
|
||||
solve::<1000000>(input)
|
||||
}
|
||||
|
||||
aoc::main!(2023, 11, ex: 1);
|
|
@ -252,3 +252,6 @@ path = "2023/09.rs"
|
|||
[[bin]]
|
||||
name = "2023_10"
|
||||
path = "2023/10.rs"
|
||||
[[bin]]
|
||||
name = "2023_11"
|
||||
path = "2023/11.rs"
|
||||
|
|
|
@ -11,10 +11,10 @@ pub trait IterExt: Iterator {
|
|||
Self: Sized,
|
||||
U: IntoIterator<Item = Self::Item>;
|
||||
|
||||
fn transpose(self) -> Transpose<Self>
|
||||
fn transpose(self) -> Transpose<<Self::Item as IntoIterator>::IntoIter>
|
||||
where
|
||||
Self: Sized,
|
||||
Self::Item: Iterator;
|
||||
Self::Item: IntoIterator;
|
||||
}
|
||||
|
||||
impl<I> IterExt for I
|
||||
|
@ -39,13 +39,12 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn transpose(self) -> Transpose<Self>
|
||||
fn transpose(self) -> Transpose<<Self::Item as std::iter::IntoIterator>::IntoIter>
|
||||
where
|
||||
Self::Item: Iterator,
|
||||
Self::Item: IntoIterator,
|
||||
{
|
||||
Transpose {
|
||||
iter: self,
|
||||
iterators: Vec::new(),
|
||||
iterators: self.map(|it| it.into_iter()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,26 +122,14 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Transpose<I>
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: Iterator,
|
||||
{
|
||||
iter: I,
|
||||
iterators: Vec<I::Item>,
|
||||
pub struct Transpose<I: Iterator> {
|
||||
iterators: Vec<I>,
|
||||
}
|
||||
|
||||
impl<I> Iterator for Transpose<I>
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: Iterator,
|
||||
{
|
||||
type Item = Vec<<<I as Iterator>::Item as IntoIterator>::Item>;
|
||||
impl<I: Iterator> Iterator for Transpose<I> {
|
||||
type Item = Vec<I::Item>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.iterators.is_empty() {
|
||||
self.iterators = self.iter.by_ref().collect();
|
||||
}
|
||||
self.iterators
|
||||
.iter_mut()
|
||||
.map(|it| it.next())
|
||||
|
|
10
examples/2023/11/1
Normal file
10
examples/2023/11/1
Normal file
|
@ -0,0 +1,10 @@
|
|||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
1
examples/2023/11/1.1
Normal file
1
examples/2023/11/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
374
|
1
examples/2023/11/1.2
Normal file
1
examples/2023/11/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
82000210
|
Loading…
Add table
Add a link
Reference in a new issue