1- use std:: collections:: HashSet ;
21use std:: fmt;
32use std:: iter:: once;
43
@@ -32,89 +31,20 @@ pub fn part_2() -> i64 {
3231 . all ( |line| !walls. iter ( ) . any ( |w| w. intersects ( line) ) )
3332 } ;
3433
35- let area = red_tiles
34+ red_tiles
3635 . iter ( )
3736 . copied ( )
3837 . array_combinations ( )
3938 . filter ( |[ a, b] | valid_rectangle ( * a, * b) )
4039 . map ( |[ a, b] | rectangular_area ( a, b) )
4140 . max ( )
42- . unwrap ( ) ;
43-
44- let matching_rects = red_tiles
45- . iter ( )
46- . copied ( )
47- . array_combinations ( )
48- . map ( |[ a, b] | ( a, b, rectangular_area ( a, b) ) )
49- . filter ( |( _, _, ar) | * ar == area)
50- . filter ( |( a, b, _) | valid_rectangle ( * a, * b) )
51- . collect_vec ( ) ;
52-
53- println ! ( "The largest rectangle has an area of {area}. Matching rectangles:" ) ;
54- for ( a, b, _) in matching_rects {
55- println ! ( " {a} - {b}" ) ;
56- }
57-
58- // let line_pts = |line: GridLine| {
59- // match line {
60- // GridLine::Horizontal { y, left_x, right_x } => {
61- // (left_x..=right_x).map(|x| Pt::new(x, y)).collect_vec()
62- // },
63- // GridLine::Vertical { x, top_y, bottom_y } => {
64- // (top_y..=bottom_y).map(|y| Pt::new(x, y)).collect_vec()
65- // },
66- // GridLine::Point(p) => vec![p],
67- // }
68- // };
69- //
70- // let red_tiles_s: HashSet<_> = red_tiles.iter().copied().collect();
71- // let path_s: HashSet<_> = red_tiles
72- // .iter()
73- // .copied()
74- // .chain(once(red_tiles[0]))
75- // .tuple_windows()
76- // .flat_map(|(a, b)| line_pts(GridLine::from_endpoints(a, b)))
77- // .collect();
78- // let walls_s: HashSet<_> = walls
79- // .iter()
80- // .flat_map(|w| line_pts(*w))
81- // .collect();
82- //
83- // let max_x = red_tiles.iter().map(|p| p.x).max().unwrap() + 3;
84- // let max_y = red_tiles.iter().map(|p| p.y).max().unwrap() + 3;
85- //
86- // for y in 0..=max_y {
87- // for x in 0..=max_x {
88- // let p = Pt::new(x, y);
89- // if red_tiles_s.contains(&p) {
90- // print!("#");
91- // } else if walls_s.contains(&p) {
92- // print!("!");
93- // } else if path_s.contains(&p) {
94- // print!("X");
95- // } else {
96- // print!(".");
97- // }
98- // }
99- // println!();
100- // }
101- // println!();
102-
103- area
41+ . unwrap ( )
10442}
10543
10644#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
10745enum GridLine {
108- Horizontal {
109- y : i64 ,
110- left_x : i64 ,
111- right_x : i64 ,
112- } ,
113- Vertical {
114- x : i64 ,
115- top_y : i64 ,
116- bottom_y : i64 ,
117- } ,
46+ Horizontal { y : i64 , left_x : i64 , right_x : i64 } ,
47+ Vertical { x : i64 , top_y : i64 , bottom_y : i64 } ,
11848 Point ( Pt ) ,
11949}
12050
@@ -135,7 +65,7 @@ impl GridLine {
13565 Self :: Horizontal { y, left_x : left_x - len, right_x }
13666 } ,
13767 ( Self :: Horizontal { y, left_x, right_x } , Direction4 :: Right ) => {
138- Self :: Horizontal { y, left_x, right_x : right_x + len}
68+ Self :: Horizontal { y, left_x, right_x : right_x + len }
13969 } ,
14070 ( Self :: Vertical { x, top_y, bottom_y } , Direction4 :: Up ) => {
14171 Self :: Vertical { x, top_y : top_y - len, bottom_y }
@@ -154,16 +84,16 @@ impl GridLine {
15484
15585 fn intersects ( self , rhs : Self ) -> bool {
15686 match ( self , rhs) {
157- ( Self :: Horizontal { y, left_x, right_x } , Self :: Vertical { x, top_y, bottom_y } ) |
158- ( Self :: Vertical { x, top_y, bottom_y } , Self :: Horizontal { y, left_x, right_x } ) => {
87+ ( Self :: Horizontal { y, left_x, right_x } , Self :: Vertical { x, top_y, bottom_y } )
88+ | ( Self :: Vertical { x, top_y, bottom_y } , Self :: Horizontal { y, left_x, right_x } ) => {
15989 ( top_y..=bottom_y) . contains ( & y) && ( left_x..=right_x) . contains ( & x)
16090 } ,
161- ( Self :: Horizontal { y, left_x, right_x } , Self :: Point ( p) ) |
162- ( Self :: Point ( p) , Self :: Horizontal { y, left_x, right_x } ) => {
91+ ( Self :: Horizontal { y, left_x, right_x } , Self :: Point ( p) )
92+ | ( Self :: Point ( p) , Self :: Horizontal { y, left_x, right_x } ) => {
16393 p. y == y && ( left_x..=right_x) . contains ( & p. x )
16494 } ,
165- ( Self :: Vertical { x, top_y, bottom_y } , Self :: Point ( p) ) |
166- ( Self :: Point ( p) , Self :: Vertical { x, top_y, bottom_y } ) => {
95+ ( Self :: Vertical { x, top_y, bottom_y } , Self :: Point ( p) )
96+ | ( Self :: Point ( p) , Self :: Vertical { x, top_y, bottom_y } ) => {
16797 p. x == x && ( top_y..=bottom_y) . contains ( & p. y )
16898 } ,
16999 _ => false ,
0 commit comments