tangled
alpha
login
or
join now
bwc9876.dev
/
advent
0
fork
atom
Advent of Code solutions
0
fork
atom
overview
issues
pulls
pipelines
Day 9 🫩
bwc9876.dev
3 months ago
4ee1fdec
5553355e
verified
This commit was signed with the committer's
known signature
.
bwc9876.dev
SSH Key Fingerprint:
SHA256:DanMEP/RNlSC7pAVbnXO6wzQV00rqyKj053tz4uH5gQ=
+88
-5
3 changed files
expand all
collapse all
unified
split
years
2025
src
day_9.rs
examples
day_9
1.txt
2.txt
+72
-5
years/2025/src/day_9.rs
···
1
1
use advent_core::{day_stuff, ex_for_day, Day};
2
2
+
use utils::pos::Position;
2
3
3
4
pub struct Day9;
4
5
5
6
impl Day for Day9 {
6
6
-
day_stuff!(9, "", "");
7
7
+
day_stuff!(9, "50", "24", Vec<Position>);
8
8
+
9
9
+
fn part_1(input: Self::Input) -> Option<String> {
10
10
+
let ans = input
11
11
+
.iter()
12
12
+
.enumerate()
13
13
+
.flat_map(|(i, a)| {
14
14
+
input.iter().skip(i + 1).map(|b| {
15
15
+
let p = (*b - *a).abs();
16
16
+
(p.x + 1) * (p.y + 1)
17
17
+
})
18
18
+
})
19
19
+
.max()
20
20
+
.unwrap();
21
21
+
22
22
+
Some(ans.to_string())
23
23
+
}
24
24
+
25
25
+
fn part_2(mut input: Self::Input) -> Option<String> {
26
26
+
let mut max = 0;
27
27
+
28
28
+
input.push(*input.first().unwrap());
29
29
+
30
30
+
let lines = input.windows(2).map(|w| (w[0], w[1])).collect::<Vec<_>>();
31
31
+
32
32
+
for (i, a) in input.iter().enumerate() {
33
33
+
// println!("Start {} ({}/{})", *a, i + 1, input.len());
34
34
+
for b in input.iter().skip(i + 1) {
35
35
+
let p = (*b - *a).abs();
36
36
+
let area = (p.x + 1) * (p.y + 1);
37
37
+
38
38
+
if area > max {
39
39
+
let check = lines.iter().any(|&l| {
40
40
+
let (cons, b1, b2, c1, c2, r1, r2) = if l.0.x == l.1.x {
41
41
+
(l.0.x, a.x, b.x, l.0.y, l.1.y, a.y, b.y)
42
42
+
} else {
43
43
+
(l.0.y, a.y, b.y, l.0.x, l.1.x, a.x, b.x)
44
44
+
};
45
45
+
46
46
+
let (lower_cons, upper_cons) = (b1.min(b2), b1.max(b2));
47
47
+
let (line_range_lower, line_range_upper) = (c1.min(c2), c1.max(c2));
48
48
+
let (rect_range_lower, rect_range_upper) = (r1.min(r2), r1.max(r2));
49
49
+
50
50
+
let cons_in_range = cons > lower_cons && cons < upper_cons;
51
51
+
let ranges_overlap = if line_range_lower < rect_range_lower {
52
52
+
line_range_upper > rect_range_lower
53
53
+
} else {
54
54
+
rect_range_upper > line_range_lower
55
55
+
};
56
56
+
57
57
+
cons_in_range && ranges_overlap
58
58
+
});
7
59
8
8
-
fn part_1(_input: Self::Input) -> Option<String> {
9
9
-
None
60
60
+
if !check {
61
61
+
max = area;
62
62
+
}
63
63
+
}
64
64
+
}
65
65
+
}
66
66
+
67
67
+
Some(max.to_string())
10
68
}
11
69
12
12
-
fn part_2(_input: Self::Input) -> Option<String> {
13
13
-
None
70
70
+
fn parse_input(input: &str) -> Self::Input {
71
71
+
input
72
72
+
.lines()
73
73
+
.map(|l| {
74
74
+
let (x, y) = l.split_once(',').unwrap();
75
75
+
Position {
76
76
+
x: x.parse().unwrap(),
77
77
+
y: y.parse().unwrap(),
78
78
+
}
79
79
+
})
80
80
+
.collect()
14
81
}
15
82
}
+8
years/2025/src/examples/day_9/1.txt
···
1
1
+
7,1
2
2
+
11,1
3
3
+
11,7
4
4
+
9,7
5
5
+
9,5
6
6
+
2,5
7
7
+
2,3
8
8
+
7,3
+8
years/2025/src/examples/day_9/2.txt
···
1
1
+
7,1
2
2
+
11,1
3
3
+
11,7
4
4
+
9,7
5
5
+
9,5
6
6
+
2,5
7
7
+
2,3
8
8
+
7,3