From 172b33b63c88abc27deb5e987321f980c85e8fe0 Mon Sep 17 00:00:00 2001 From: Quentin Boyer Date: Wed, 10 Dec 2025 01:37:13 +0100 Subject: [PATCH] Switch to u32 to reduce the hash complexity --- src/bin/day9.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/bin/day9.rs b/src/bin/day9.rs index 8c4af39..b79d146 100644 --- a/src/bin/day9.rs +++ b/src/bin/day9.rs @@ -10,8 +10,8 @@ use bstr::BString; #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)] pub struct Point2 { - x: u64, - y: u64, + x: u32, + y: u32, } type Parsed = Vec; @@ -32,7 +32,7 @@ pub fn parsing(input: &BString) -> color_eyre::Result { .collect() } -fn rect_area(a: &Point2, b: &Point2) -> u64 { +fn rect_area(a: &Point2, b: &Point2) -> u32 { (a.x.abs_diff(b.x) + 1) * (a.y.abs_diff(b.y) + 1) } @@ -52,11 +52,11 @@ pub fn part1(input: Parsed) { struct Polygon { points: Vec, - x_values: Vec, - y_values: Vec, + x_values: Vec, + y_values: Vec, - vertical_edges_by_x_slot: Vec>>, - horizontal_edges_by_y_slot: Vec>>, + vertical_edges_by_x_slot: Vec>>, + horizontal_edges_by_y_slot: Vec>>, } impl Polygon { @@ -167,11 +167,11 @@ impl Polygon { &self.points } - pub fn x_values(&self) -> &[u64] { + pub fn x_values(&self) -> &[u32] { &self.x_values } - pub fn y_values(&self) -> &[u64] { + pub fn y_values(&self) -> &[u32] { &self.y_values } @@ -204,14 +204,14 @@ fn horizontal_edges_contained( a: Point2, b: Point2, cache: &mut HashMap, - edge_cache: &mut HashMap<(u64, (u64, u64)), bool>, + edge_cache: &mut HashMap<(u32, (u32, u32)), bool>, ) -> bool { assert_ne!(a.x, b.x); let min_x = a.x.min(b.x); let max_x = a.x.max(b.x); - let y_values: &[u64] = if a.y == b.y { &[a.y] } else { &[a.y, b.y] }; + let y_values: &[u32] = if a.y == b.y { &[a.y] } else { &[a.y, b.y] }; let mut contains = |point: Point2| match cache.get(&point) { Some(&v) => v, @@ -278,14 +278,14 @@ fn vertical_edges_contain( a: Point2, b: Point2, cache: &mut HashMap, - edge_cache: &mut HashMap<(u64, (u64, u64)), bool>, + edge_cache: &mut HashMap<(u32, (u32, u32)), bool>, ) -> bool { assert_ne!(a.y, b.y); let min_y = a.y.min(b.y); let max_y = a.y.max(b.y); - let x_values: &[u64] = if a.x == b.x { &[a.x] } else { &[a.x, b.x] }; + let x_values: &[u32] = if a.x == b.x { &[a.x] } else { &[a.x, b.x] }; let mut contains = |point: Point2| match cache.get(&point) { Some(&v) => v,