Skip to content

Commit fdcc1b8

Browse files
committed
Year 2025 Day 9
1 parent d32dd68 commit fdcc1b8

File tree

7 files changed

+125
-4
lines changed

7 files changed

+125
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8585
| 6 | [Trash Compactor](https://adventofcode.com/2025/day/6) | [Source](src/year2025/day06.rs) | 20 |
8686
| 7 | [Laboratories](https://adventofcode.com/2025/day/7) | [Source](src/year2025/day07.rs) | 5 |
8787
| 8 | [Playground](https://adventofcode.com/2025/day/8) | [Source](src/year2025/day08.rs) | 527 |
88+
| 9 | [Movie Theater](https://adventofcode.com/2025/day/9) | [Source](src/year2025/day09.rs) | 46000 |
8889

8990
## 2024
9091

benches/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ benchmark!(year2024
9595
);
9696

9797
benchmark!(year2025
98-
day01, day02, day03, day04, day05, day06, day07, day08
98+
day01, day02, day03, day04, day05, day06, day07, day08, day09
9999
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ library!(year2024 "Locate the Chief Historian in time for the big Christmas slei
7474
);
7575

7676
library!(year2025 "Finish the North Pole decorations in time for Christmas."
77-
day01, day02, day03, day04, day05, day06, day07, day08
77+
day01, day02, day03, day04, day05, day06, day07, day08, day09
7878
);

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ run!(year2024
141141
);
142142

143143
run!(year2025
144-
day01, day02, day03, day04, day05, day06, day07, day08
144+
day01, day02, day03, day04, day05, day06, day07, day08, day09
145145
);

src/year2025/day09.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use crate::util::grid::*;
2+
use crate::util::iter::*;
3+
use crate::util::parse::*;
4+
use crate::util::point::*;
5+
use std::collections::{BTreeMap, BTreeSet, VecDeque};
6+
7+
type Tile = [u64; 2];
8+
9+
pub fn parse(input: &str) -> Vec<Tile> {
10+
input.iter_unsigned::<u64>().chunk::<2>().collect()
11+
}
12+
13+
pub fn part1(tiles: &[Tile]) -> u64 {
14+
let mut area = 0;
15+
16+
for (i, &[x1, y1]) in tiles.iter().enumerate() {
17+
for &[x2, y2] in tiles.iter().skip(i + 1) {
18+
let dx = x1.abs_diff(x2) + 1;
19+
let dy = y1.abs_diff(y2) + 1;
20+
area = area.max(dx * dy);
21+
}
22+
}
23+
24+
area
25+
}
26+
27+
pub fn part2(tiles: &[Tile]) -> u64 {
28+
let mut xs: BTreeSet<_> = tiles.iter().map(|&[x, _]| x).collect();
29+
xs.insert(0);
30+
xs.insert(u64::MAX);
31+
let shrink_x: BTreeMap<_, _> = xs.into_iter().enumerate().map(|(i, x)| (x, i as i32)).collect();
32+
33+
let mut ys: BTreeSet<_> = tiles.iter().map(|&[_, y]| y).collect();
34+
ys.insert(0);
35+
ys.insert(u64::MAX);
36+
let shrink_y: BTreeMap<_, _> = ys.into_iter().enumerate().map(|(i, y)| (y, i as i32)).collect();
37+
38+
let mut grid = Grid::new(shrink_x.len() as i32, shrink_y.len() as i32, b'X');
39+
40+
for i in 0..tiles.len() {
41+
let [x1, y1] = tiles[i];
42+
let [x2, y2] = tiles[(i + 1) % tiles.len()];
43+
44+
let x1 = shrink_x[&x1];
45+
let x2 = shrink_x[&x2];
46+
let y1 = shrink_y[&y1];
47+
let y2 = shrink_y[&y2];
48+
49+
for x in x1.min(x2)..=x1.max(x2) {
50+
for y in y1.min(y2)..=y1.max(y2) {
51+
grid[Point::new(x, y)] = b'#';
52+
}
53+
}
54+
}
55+
56+
let mut todo = VecDeque::new();
57+
todo.push_back(ORIGIN);
58+
59+
while let Some(point) = todo.pop_front() {
60+
for next in ORTHOGONAL.map(|o| point + o) {
61+
if grid.contains(next) && grid[next] == b'X' {
62+
grid[next] = b'.';
63+
todo.push_back(next);
64+
}
65+
}
66+
}
67+
68+
let mut area = 0;
69+
70+
for i in 0..tiles.len() {
71+
'outer: for j in i + 1..tiles.len() {
72+
let [x1, y1] = tiles[i];
73+
let [x2, y2] = tiles[j];
74+
75+
let x1 = shrink_x[&x1];
76+
let x2 = shrink_x[&x2];
77+
let y1 = shrink_y[&y1];
78+
let y2 = shrink_y[&y2];
79+
80+
for x in x1.min(x2)..=x1.max(x2) {
81+
for y in y1.min(y2)..=y1.max(y2) {
82+
if grid[Point::new(x, y)] == b'.' {
83+
continue 'outer;
84+
}
85+
}
86+
}
87+
88+
let [x1, y1] = tiles[i];
89+
let [x2, y2] = tiles[j];
90+
let dx = x1.abs_diff(x2) + 1;
91+
let dy = y1.abs_diff(y2) + 1;
92+
area = area.max(dx * dy);
93+
}
94+
}
95+
96+
area
97+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ test!(year2024
8787
);
8888

8989
test!(year2025
90-
day01, day02, day03, day04, day05, day06, day07, day08
90+
day01, day02, day03, day04, day05, day06, day07, day08, day09
9191
);

tests/year2025/day09.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use aoc::year2025::day09::*;
2+
3+
const EXAMPLE: &str = "\
4+
7,1
5+
11,1
6+
11,7
7+
9,7
8+
9,5
9+
2,5
10+
2,3
11+
7,3";
12+
13+
#[test]
14+
fn part1_test() {
15+
let input = parse(EXAMPLE);
16+
assert_eq!(part1(&input), 50);
17+
}
18+
19+
#[test]
20+
fn part2_test() {
21+
let input = parse(EXAMPLE);
22+
assert_eq!(part2(&input), 24);
23+
}

0 commit comments

Comments
 (0)