-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd09.rs
More file actions
161 lines (124 loc) Β· 3.83 KB
/
d09.rs
File metadata and controls
161 lines (124 loc) Β· 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::cmp::{max, min};
use crate::Day;
pub struct Day09 {}
impl Day for Day09 {
fn year(&self) -> u16 {
2025
}
fn day(&self) -> u8 {
9
}
fn part_one(&self) -> String {
let input = self.read_default_input();
let red_tiles: Vec<Point2D> = parse_input(&input);
let mut largest_area = 0;
for i in 0..red_tiles.len() {
for j in (i + 1)..red_tiles.len() {
let rect = Rectangle::from(&(&red_tiles[i], &red_tiles[j]));
let area = area(&rect);
if area > largest_area {
largest_area = area;
}
}
}
largest_area.to_string()
}
fn part_two(&self) -> String {
let input = self.read_default_input();
let red_tiles: Vec<Point2D> = parse_input(&input);
let mut edges = Vec::new();
for i in 0..red_tiles.len() {
let edge = (&red_tiles[i], &red_tiles[(i + 1) % red_tiles.len()]);
edges.push(edge);
}
let mut largest_area = 0;
for i in 0..red_tiles.len() {
for j in (i + 1)..red_tiles.len() {
let rectangle = Rectangle::from(&(&red_tiles[i], &red_tiles[j]));
let area = area(&rectangle);
if area <= largest_area {
continue;
}
let mut is_cut_by_edge = false;
for edge in &edges {
if is_cut_by_segment(&rectangle, edge) {
is_cut_by_edge = true;
break;
}
}
if is_cut_by_edge {
continue;
}
largest_area = area;
}
}
largest_area.to_string()
}
}
#[derive(Debug, Clone)]
struct Point2D {
x: i64,
y: i64,
}
#[derive(Debug)]
struct Rectangle(Point2D, Point2D);
fn area(rect: &Rectangle) -> i64 {
let width = (rect.1.x - rect.0.x).abs() + 1;
let height = (rect.1.y - rect.0.y).abs() + 1;
width * height
}
fn bounds(rect: &Rectangle) -> (i64, i64, i64, i64) {
let (min_x, max_x) = min_max(rect.0.x, rect.1.x);
let (min_y, max_y) = min_max(rect.0.y, rect.1.y);
(min_x, max_x, min_y, max_y)
}
fn is_cut_by_segment(rect: &Rectangle, segment: &(&Point2D, &Point2D)) -> bool {
let (min_x, max_x, min_y, max_y) = bounds(rect);
let (point_a, point_b) = segment;
// Vertical
if point_a.x == point_b.x {
let line_x = point_a.x;
if line_x <= min_x || line_x >= max_x {
return false;
}
let (segment_min_y, segment_max_y) = min_max(point_a.y, point_b.y);
return segment_max_y > min_y && segment_min_y < max_y;
}
// Horizontal
if point_a.y == point_b.y {
let line_y = point_a.y;
if line_y <= min_y || line_y >= max_y {
return false;
}
let (segment_min_x, segment_max_x) = min_max(point_a.x, point_b.x);
return segment_max_x > min_x && segment_min_x < max_x;
}
false
}
impl From<&(&Point2D, &Point2D)> for Rectangle {
fn from(value: &(&Point2D, &Point2D)) -> Self {
Self(value.0.clone(), value.1.clone())
}
}
fn parse_input(input: &str) -> Vec<Point2D> {
input
.lines()
.map(|line| {
let [x, y] = line
.split(",")
.map(|number| {
number
.parse::<i64>()
.expect("numbers on the input should be valid i64")
})
.collect::<Vec<i64>>()[..]
else {
panic!("Invalid line format");
};
Point2D { x, y }
})
.collect::<Vec<_>>()
}
fn min_max(a: i64, b: i64) -> (i64, i64) {
(min(a, b), max(a, b))
}