here and there optimizations

This commit is contained in:
2025-12-09 18:50:10 +01:00
parent 5d9aa6fc4c
commit 0481138294

View File

@@ -8,40 +8,38 @@ use std::ops::Range;
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "Rust" fn challenge_usize(buf: &[u8]) -> usize { pub extern "Rust" fn challenge_usize(buf: &[u8]) -> usize {
// I do see how to make this in idk if O(n) or O(nlogn), but ima O(n^2) just at first
let coords = buf[..(buf.len() - 1)] let coords = buf[..(buf.len() - 1)]
.split(|&b| b == b'\n') .split(|&b| b == b'\n')
.map(parse_ln) .map(parse_ln)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// assuming each coord is contiguous to the prev one // assuming each coord is contiguous to the prev one
let edges = coords let mut edges = coords
.iter() .iter()
.cloned() .cloned()
.chain([coords[0]]) .chain([coords[0]])
.map_windows(|&[a, b]| (a, b)) .map_windows(|&[a, b]| (a, b))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut area = 0; edges.sort_by(|(a1, a2), (b1, b2)| {
for coor1 in &coords { (a1.0.abs_diff(a2.0) + a1.1.abs_diff(a2.1))
for coor2 in &coords { .cmp(&(b1.0.abs_diff(b2.0) + b1.1.abs_diff(b2.1)))
});
let mut max_area = 0;
for (i, coor1) in coords.iter().enumerate() {
for coor2 in coords.iter().skip(i) {
let dx = coor1.0.abs_diff(coor2.0) + 1; let dx = coor1.0.abs_diff(coor2.0) + 1;
let dy = coor1.1.abs_diff(coor2.1) + 1; let dy = coor1.1.abs_diff(coor2.1) + 1;
let this_area = dx * dy; let area = dx * dy;
// println!("{coor1:?}, {coor2:?}");
if is_really_contained((*coor1, *coor2), &edges) { if is_really_contained((*coor1, *coor2), &edges) {
area = area.max(this_area); max_area = max_area.max(area);
} }
// println!(
// "{},{} {},{} | {this_area}",
// coor1.0, coor1.1, coor2.0, coor2.1
// );
} }
} }
area max_area
} }
/// If any bouding vertex is well within (not sitting on a rectangle's edge), the rectangle is not /// If any bouding vertex is well within (not sitting on a rectangle's edge), the rectangle is not
@@ -60,16 +58,18 @@ fn is_really_contained(
// Optimization, no need to check each range's point // Optimization, no need to check each range's point
for (edge1, edge2) in edges { for (edge1, edge2) in edges {
if edge1.0 == edge2.0 && xran.contains(&edge1.0) { if edge1.0 == edge2.0
if rangeoverlap(&mkrange(edge1.1, edge2.1), &yran) { && xran.contains(&edge1.0)
return false; && rangeoverlap(&mkrange(edge1.1, edge2.1), &yran)
} {
return false;
} }
if edge1.1 == edge2.1 && yran.contains(&edge1.1) { if edge1.1 == edge2.1
if rangeoverlap(&mkrange(edge1.0, edge2.0), &xran) { && yran.contains(&edge1.1)
return false; && rangeoverlap(&mkrange(edge1.0, edge2.0), &xran)
} {
return false;
} }
} }