-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
51 lines (43 loc) · 1.21 KB
/
11.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);