Skip to content

Commit c70ddce

Browse files
committed
chore: WIP for y2025::day_09::part_2
1 parent 5a953af commit c70ddce

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

.run/run_aoc.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="run_aoc" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
33
<option name="buildProfileId" value="dev" />
4-
<option name="command" value="run --package aoclp_solutions --bin aoclp_solutions -- --year 2025 --day 8" />
4+
<option name="command" value="run --package aoclp_solutions --bin aoclp_solutions -- --year 2025 --day 9" />
55
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
66
<envs />
77
<option name="emulateTerminal" value="true" />

aoclp/src/positioning/pt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ where
178178
}
179179

180180
/// Returns the size of the rectangle formed between two points in 2D space,
181-
/// as if the points were the rectangle's top-left and bottom-right corners.
181+
/// as if the points were two of the rectangle's opposing corners.
182182
pub fn rectangular_area<T>(a: Pt<T>, b: Pt<T>) -> T
183183
where
184184
T: Signed,

aoclp_solutions/src/y2025/day_09.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::collections::{HashMap, HashSet};
2-
use std::hash::Hash;
3-
use std::iter::successors;
1+
use std::cmp::{max, min};
2+
use std::collections::{HashSet, VecDeque};
3+
use std::iter::{once, successors};
44

55
use aoclp::positioning::pt::{rectangular_area, Pt};
66
use aoclp::solvers_impl::input::safe_get_input_as_many;
77
use itertools::Itertools;
8-
use strum::EnumIs;
8+
use aoclp::num::Zero;
99

1010
pub fn part_1() -> i64 {
1111
input()
@@ -16,37 +16,56 @@ pub fn part_1() -> i64 {
1616
.unwrap()
1717
}
1818

19-
pub fn part_2() -> usize {
20-
0
21-
}
19+
pub fn part_2() -> i64 {
20+
let red_tiles = input();
2221

23-
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, EnumIs)]
24-
enum Tile {
25-
#[default]
26-
Pink,
27-
Red,
28-
Green,
29-
}
22+
let path: HashSet<_> = red_tiles
23+
.iter()
24+
.copied()
25+
.chain(once(red_tiles[0]))
26+
.enumerate()
27+
.tuple_windows()
28+
.flat_map(|((ia, a), (ib, b))| {
29+
let displacement = Pt::new((b.x - a.x).signum(), (b.y - a.y).signum());
30+
if displacement.x != 0 && displacement.y != 0 {
31+
panic!("Pair {} (#{ia}), {} (#{ib}) have wrong displacement", a.to_string(), b.to_string())
32+
}
33+
successors(Some(a), move |&p| (p != b).then_some(p + displacement))
34+
.chain(once(b))
35+
})
36+
.collect();
3037

31-
#[derive(Debug, Clone)]
32-
struct Floor(HashMap<Pt, Tile>);
38+
let floor_width = red_tiles.iter().map(|p| p.x).max().unwrap() + 2;
39+
let floor_height = red_tiles.iter().map(|p| p.y).max().unwrap() + 2;
3340

34-
impl Default for Floor {
35-
fn default() -> Self {
36-
let tiles = input();
41+
let mut lava = HashSet::new();
42+
let mut q = VecDeque::new();
43+
q.push_back(Pt::zero());
44+
while let Some(p) = q.pop_front() {
45+
if !p.within(0..floor_width, 0..floor_height) || path.contains(&p) {
46+
continue;
47+
}
48+
if lava.insert(p) {
49+
q.extend(p.four_neighbours());
50+
}
51+
}
3752

38-
let _path: HashSet<_> = tiles
39-
.iter()
40-
.tuple_windows()
41-
.flat_map(|(a, b)| {
42-
let displacement = Pt::new((b.x - a.x).signum(), (b.y - a.y).signum());
43-
let b = *b;
44-
successors(Some(*a), move |&p| (p != b).then_some(p + displacement))
53+
let safe_rectangle = |a: Pt, b: Pt| -> bool {
54+
(min(a.y, b.y)..=max(a.y, b.y))
55+
.into_iter()
56+
.flat_map(|y| {
57+
(min(a.x, b.x)..=max(a.x, b.x)).into_iter().map(move |x| Pt::new(x, y))
4558
})
46-
.collect();
59+
.all(|p| !lava.contains(&p))
60+
};
4761

48-
todo!()
49-
}
62+
red_tiles
63+
.into_iter()
64+
.array_combinations()
65+
.filter(|&[a, b]| safe_rectangle(a, b))
66+
.map(|[a, b]| rectangular_area(a, b))
67+
.max()
68+
.unwrap()
5069
}
5170

5271
fn input() -> Vec<Pt> {

0 commit comments

Comments
 (0)