Switch to u32 to reduce the hash complexity

This commit is contained in:
Quentin Boyer 2025-12-10 01:37:13 +01:00
parent a8ee7f78f6
commit 172b33b63c

View file

@ -10,8 +10,8 @@ use bstr::BString;
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)] #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub struct Point2 { pub struct Point2 {
x: u64, x: u32,
y: u64, y: u32,
} }
type Parsed = Vec<Point2>; type Parsed = Vec<Point2>;
@ -32,7 +32,7 @@ pub fn parsing(input: &BString) -> color_eyre::Result<Parsed> {
.collect() .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) (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 { struct Polygon {
points: Vec<Point2>, points: Vec<Point2>,
x_values: Vec<u64>, x_values: Vec<u32>,
y_values: Vec<u64>, y_values: Vec<u32>,
vertical_edges_by_x_slot: Vec<Vec<RangeInclusive<u64>>>, vertical_edges_by_x_slot: Vec<Vec<RangeInclusive<u32>>>,
horizontal_edges_by_y_slot: Vec<Vec<RangeInclusive<u64>>>, horizontal_edges_by_y_slot: Vec<Vec<RangeInclusive<u32>>>,
} }
impl Polygon { impl Polygon {
@ -167,11 +167,11 @@ impl Polygon {
&self.points &self.points
} }
pub fn x_values(&self) -> &[u64] { pub fn x_values(&self) -> &[u32] {
&self.x_values &self.x_values
} }
pub fn y_values(&self) -> &[u64] { pub fn y_values(&self) -> &[u32] {
&self.y_values &self.y_values
} }
@ -204,14 +204,14 @@ fn horizontal_edges_contained(
a: Point2, a: Point2,
b: Point2, b: Point2,
cache: &mut HashMap<Point2, bool>, cache: &mut HashMap<Point2, bool>,
edge_cache: &mut HashMap<(u64, (u64, u64)), bool>, edge_cache: &mut HashMap<(u32, (u32, u32)), bool>,
) -> bool { ) -> bool {
assert_ne!(a.x, b.x); assert_ne!(a.x, b.x);
let min_x = a.x.min(b.x); let min_x = a.x.min(b.x);
let max_x = a.x.max(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) { let mut contains = |point: Point2| match cache.get(&point) {
Some(&v) => v, Some(&v) => v,
@ -278,14 +278,14 @@ fn vertical_edges_contain(
a: Point2, a: Point2,
b: Point2, b: Point2,
cache: &mut HashMap<Point2, bool>, cache: &mut HashMap<Point2, bool>,
edge_cache: &mut HashMap<(u64, (u64, u64)), bool>, edge_cache: &mut HashMap<(u32, (u32, u32)), bool>,
) -> bool { ) -> bool {
assert_ne!(a.y, b.y); assert_ne!(a.y, b.y);
let min_y = a.y.min(b.y); let min_y = a.y.min(b.y);
let max_y = a.y.max(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) { let mut contains = |point: Point2| match cache.get(&point) {
Some(&v) => v, Some(&v) => v,