From af6cba144581fd541680769d359d2b39eed8942e Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 11 Dec 2023 10:51:54 +0100 Subject: [PATCH] =?UTF-8?q?day(11):=20use=20=E2=80=B9usize=E2=80=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- src/bin/day11.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/bin/day11.rs b/src/bin/day11.rs index efb48f4..b1818f4 100644 --- a/src/bin/day11.rs +++ b/src/bin/day11.rs @@ -1,20 +1,23 @@ -use std::collections::HashSet; +use std::{ + cmp::{max, min}, + collections::HashSet, +}; use itertools::iproduct; use aoc_2023::*; -type Output1 = u64; +type Output1 = usize; type Output2 = Output1; struct Day11 { - galaxies: Vec<(i64, i64)>, + galaxies: Vec<(usize, usize)>, empty_ys: HashSet, empty_xs: HashSet, } impl Day11 { - fn solve(&self, factor: u64) -> u64 { + fn solve(&self, factor: usize) -> usize { let to_add = factor - 1; (0..self.galaxies.len() - 1) @@ -28,24 +31,26 @@ impl Day11 { let mut xd = x0.abs_diff(x1); 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 { - if (min_x as usize) < *x && *x < max_x as usize { + if min_x < *x && *x < max_x { xd += to_add; } } for y in &self.empty_ys { - if (y0 as usize) < *y && *y < (y1 as usize) { + if y0 < *y && *y < y1 { yd += to_add; } } xd + yd }) - .sum::() + .sum::() }) - .sum::() + .sum() } } @@ -55,9 +60,7 @@ impl Solution for Day11 { let map = lines.iter().map(|l| l.chars().collect_vec()).collect_vec(); let galaxies = iproduct!((0..map.len()), (0..map[0].len())) - .filter_map(|(y, x)| { - (map[y][x] != '.').then(|| (y.try_into().unwrap(), x.try_into().unwrap())) - }) + .filter_map(|(y, x)| (map[y][x] != '.').then_some((y, x))) .collect(); let empty_ys = map