1
0
Fork 0

day(11): use ‹usize›

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-11 10:51:54 +01:00
parent 09e0d81ffe
commit af6cba1445
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,20 +1,23 @@
use std::collections::HashSet; use std::{
cmp::{max, min},
collections::HashSet,
};
use itertools::iproduct; use itertools::iproduct;
use aoc_2023::*; use aoc_2023::*;
type Output1 = u64; type Output1 = usize;
type Output2 = Output1; type Output2 = Output1;
struct Day11 { struct Day11 {
galaxies: Vec<(i64, i64)>, galaxies: Vec<(usize, usize)>,
empty_ys: HashSet<usize>, empty_ys: HashSet<usize>,
empty_xs: HashSet<usize>, empty_xs: HashSet<usize>,
} }
impl Day11 { impl Day11 {
fn solve(&self, factor: u64) -> u64 { fn solve(&self, factor: usize) -> usize {
let to_add = factor - 1; let to_add = factor - 1;
(0..self.galaxies.len() - 1) (0..self.galaxies.len() - 1)
@ -28,24 +31,26 @@ impl Day11 {
let mut xd = x0.abs_diff(x1); let mut xd = x0.abs_diff(x1);
let mut yd = y0.abs_diff(y1); let mut yd = y0.abs_diff(y1);
let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) }; let min_x = min(x0, x1);
let max_x = max(x0, x1);
for x in &self.empty_xs { for x in &self.empty_xs {
if (min_x as usize) < *x && *x < max_x as usize { if min_x < *x && *x < max_x {
xd += to_add; xd += to_add;
} }
} }
for y in &self.empty_ys { for y in &self.empty_ys {
if (y0 as usize) < *y && *y < (y1 as usize) { if y0 < *y && *y < y1 {
yd += to_add; yd += to_add;
} }
} }
xd + yd xd + yd
}) })
.sum::<u64>() .sum::<usize>()
}) })
.sum::<u64>() .sum()
} }
} }
@ -55,9 +60,7 @@ impl Solution<Output1, Output2> for Day11 {
let map = lines.iter().map(|l| l.chars().collect_vec()).collect_vec(); let map = lines.iter().map(|l| l.chars().collect_vec()).collect_vec();
let galaxies = iproduct!((0..map.len()), (0..map[0].len())) let galaxies = iproduct!((0..map.len()), (0..map[0].len()))
.filter_map(|(y, x)| { .filter_map(|(y, x)| (map[y][x] != '.').then_some((y, x)))
(map[y][x] != '.').then(|| (y.try_into().unwrap(), x.try_into().unwrap()))
})
.collect(); .collect();
let empty_ys = map let empty_ys = map