Skip to content

Commit a5effc7

Browse files
committed
chore: y2025::day_09::part_2
1 parent 109512e commit a5effc7

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

aoclp_solutions/src/y2025/day_09.rs

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use aoclp::positioning::direction::four_points::Direction4;
66
use aoclp::positioning::pt::{rectangular_area, Pt};
77
use aoclp::solvers_impl::input::safe_get_input_as_many;
88
use itertools::{chain, Either, Itertools};
9-
use aoclp::positioning::direction::Direction;
9+
use strum::IntoEnumIterator;
10+
use aoclp::positioning::direction::{Direction, MovementDirection};
1011

1112
pub fn part_1() -> i64 {
1213
input()
@@ -40,57 +41,14 @@ pub fn part_2() -> i64 {
4041
edges.is_disjoint(&red_zone)
4142
};
4243

43-
let (a, b, area) = red_tiles
44+
red_tiles
4445
.into_iter()
4546
.array_combinations()
4647
.map(|[a, b]| (a, b, rectangular_area(a, b)))
4748
.sorted_unstable_by(|(_, _, area_a), (_, _, area_b)| area_b.cmp(area_a))
4849
.find(|&(a, b, _)| safe_rectangle(a, b))
49-
.unwrap();
50-
println!("Largest rectangle is ({a}, {b}) area: {area}");
51-
52-
// {
53-
// let red_tiles = input();
54-
// let min_x = red_tiles.iter().map(|a| a.x).min().unwrap() - 2;
55-
// let max_x = red_tiles.iter().map(|a| a.x).max().unwrap() - 2;
56-
// let min_y = red_tiles.iter().map(|a| a.y).min().unwrap() + 2;
57-
// let max_y = red_tiles.iter().map(|a| a.y).max().unwrap() + 2;
58-
// let red_tiles: HashSet<_> = red_tiles.iter().copied().collect();
59-
// let corners = vec![
60-
// Pt::new(min(a.x, b.x), min(a.y, b.y)),
61-
// Pt::new(max(a.x, b.x), min(a.y, b.y)),
62-
// Pt::new(max(a.x, b.x), max(a.y, b.y)),
63-
// Pt::new(max(a.x, b.x), min(a.y, b.y)),
64-
// Pt::new(min(a.x, b.x), min(a.y, b.y)),
65-
// ];
66-
// let edges: BTreeSet<_> = corners
67-
// .into_iter()
68-
// .tuple_windows()
69-
// .flat_map(|(a, b)| {
70-
// let displacement = Pt::new((b.x - a.x).signum(), (b.y - a.y).signum());
71-
// successors(Some(a), move |&p| (p != b).then_some(p + displacement))
72-
// })
73-
// .collect();
74-
//
75-
// for y in min_x..=max_y {
76-
// for x in min_y..=max_x {
77-
// let pt = Pt::new(x, y);
78-
// if red_tiles.contains(&pt) {
79-
// print!("#");
80-
// } else if red_zone.contains(&pt) {
81-
// print!("!");
82-
// } else if edges.contains(&pt) {
83-
// print!("O");
84-
// } else {
85-
// print!(".");
86-
// }
87-
// }
88-
// println!();
89-
// }
90-
// println!();
91-
// }
92-
93-
area
50+
.map(|(_, _, area)| area)
51+
.unwrap()
9452
}
9553

9654
fn build_red_zone(red_tiles: &[Pt]) -> impl Iterator<Item = Pt> + use<'_> {
@@ -101,30 +59,32 @@ fn build_red_zone(red_tiles: &[Pt]) -> impl Iterator<Item = Pt> + use<'_> {
10159
.next()
10260
.unwrap();
10361

104-
let get_direction = |a: Pt, b: Pt| match ((b.x - a.x).signum(), (b.y - a.y).signum()) {
105-
(1, 0) => Direction4::Right,
106-
(0, 1) => Direction4::Down,
107-
(-1, 0) => Direction4::Left,
108-
(0, -1) => Direction4::Up,
109-
_ => unreachable!(),
62+
let get_direction = |a: Pt, b: Pt| {
63+
let displacement = Pt::new((b.x - a.x).signum(), (b.y - a.y).signum());
64+
Direction4::iter().find(|&d| d.displacement() == displacement).unwrap()
11065
};
11166

11267
red_tiles
11368
.iter()
11469
.copied()
11570
.cycle()
11671
.skip_while(move |&p| p != starting_point)
117-
.take(red_tiles.len() + 1)
72+
.take(red_tiles.len() + 2)
11873
.tuple_windows()
11974
.flat_map(move |(a, b, c)| {
12075
let direction = get_direction(a, b);
12176
let turning_left = get_direction(b, c) == direction.turn_left();
12277

123-
let red_zone_path = successors(Some(a), move |&p| (p != b).then_some(p + direction))
78+
let red_zone_path = successors(Some(a), move |&p| {
79+
let next = p + direction;
80+
(next != b).then_some(next)
81+
})
12482
.skip(1)
125-
.map(move |p| p + (direction.turn_left()));
83+
.map(move |p| p + (direction.turn_left()))
84+
.collect_vec();
85+
12686
if turning_left {
127-
Either::Left(red_zone_path)
87+
Either::Left(red_zone_path.into_iter())
12888
} else {
12989
Either::Right(chain(
13090
red_zone_path,
@@ -138,6 +98,16 @@ fn build_red_zone(red_tiles: &[Pt]) -> impl Iterator<Item = Pt> + use<'_> {
13898
})
13999
}
140100

101+
const EXAMPLE: &str = "\
102+
7,1\n\
103+
11,1\n\
104+
11,7\n\
105+
9,7\n\
106+
9,5\n\
107+
2,5\n\
108+
2,3\n\
109+
7,3";
110+
141111
fn input() -> Vec<Pt> {
142112
safe_get_input_as_many(2025, 9)
143113
}

0 commit comments

Comments
 (0)