Skip to content

Commit 9f70fbb

Browse files
committed
2025 Day 9
1 parent 533a16b commit 9f70fbb

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/bin/2025_09/main.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::collections::HashSet;
2+
3+
use aoc::{common, io};
4+
5+
fn intersect_inner(boundary: &[&(usize, usize)], p1: &(usize, usize), p2: &(usize, usize)) -> bool {
6+
boundary
7+
.iter()
8+
.any(|b| b.0 > p1.0 && b.0 < p2.0 && b.1 > p1.1 && b.1 < p2.1)
9+
}
10+
11+
fn solve<const PART: usize>(input: &str) -> usize {
12+
let mut corner_pts = Vec::new();
13+
14+
for line in input.lines() {
15+
if line.is_empty() {
16+
continue;
17+
}
18+
let corners = io::tokenize_nums::<usize>(line, ",");
19+
corner_pts.push(corners);
20+
}
21+
22+
let mut boundary: HashSet<(usize, usize)> = HashSet::new();
23+
for i in 0..corner_pts.len() {
24+
for j in i + 1..corner_pts.len() {
25+
if corner_pts[i][0] == corner_pts[j][0] {
26+
let y1 = corner_pts[i][1].min(corner_pts[j][1]);
27+
let y2 = corner_pts[i][1].max(corner_pts[j][1]);
28+
for y in y1..=y2 {
29+
boundary.insert((corner_pts[i][0], y));
30+
}
31+
}
32+
33+
if corner_pts[i][1] == corner_pts[j][1] {
34+
let x1 = corner_pts[i][0].min(corner_pts[j][0]);
35+
let x2 = corner_pts[i][0].max(corner_pts[j][0]);
36+
for x in x1..=x2 {
37+
boundary.insert((x, corner_pts[i][1]));
38+
}
39+
}
40+
}
41+
}
42+
43+
let boundary_vec: Vec<&(usize, usize)> = boundary.iter().collect();
44+
45+
let mut max_area = 0;
46+
for i in 0..corner_pts.len() {
47+
for j in i + 1..corner_pts.len() {
48+
let xmin = corner_pts[i][0].min(corner_pts[j][0]);
49+
let xmax = corner_pts[i][0].max(corner_pts[j][0]);
50+
let ymin = corner_pts[i][1].min(corner_pts[j][1]);
51+
let ymax = corner_pts[i][1].max(corner_pts[j][1]);
52+
let x = xmax - xmin + 1;
53+
let y = ymax - ymin + 1;
54+
55+
if x * y <= max_area {
56+
continue;
57+
}
58+
59+
if PART == 1 {
60+
max_area = max_area.max(x * y);
61+
} else if !intersect_inner(&boundary_vec, &(xmin, ymin), &(xmax, ymax)) {
62+
//println!("{:?} {:?} => {}", corner_pts[i], corner_pts[j], x * y);
63+
max_area = max_area.max(x * y);
64+
}
65+
}
66+
}
67+
68+
max_area
69+
}
70+
71+
fn main() {
72+
if let Some(input) = common::get_input() {
73+
common::timed(&input, solve::<1>, true);
74+
common::timed(&input, solve::<2>, false);
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::*;
81+
82+
#[test]
83+
fn test_samples() {
84+
let sample_input = "7,1\n11,1\n11,7\n9,7\n9,5\n2,5\n2,3\n7,3";
85+
assert_eq!(solve::<1>(sample_input), 50);
86+
assert_eq!(solve::<2>(sample_input), 24);
87+
}
88+
}

0 commit comments

Comments
 (0)