My submissions for Advent of Code 2025
adventofcode.com/2025
rust
aoc
1use std::{cmp::Ordering, collections::BTreeMap, fs::read_to_string};
2
3use itertools::Itertools;
4
5fn main() {
6 let coords: Vec<_> = read_to_string("../input.txt")
7 .expect("failed to open input file")
8 .lines()
9 .map(|line| {
10 let (x, y) = line.split_once(',').expect("failed to parse co-ordinate");
11 (
12 x.parse::<u64>().expect("failed to parse x"),
13 y.parse::<u64>().expect("failed to parse y"),
14 )
15 })
16 .collect();
17 let mut horizontal_edges: BTreeMap<_, Vec<_>> = BTreeMap::new();
18 let mut vertical_edges: BTreeMap<_, Vec<_>> = BTreeMap::new();
19 for ((from_x, from_y), (to_x, to_y)) in coords.iter().circular_tuple_windows() {
20 if from_x == to_x {
21 vertical_edges
22 .entry(*from_x)
23 .or_default()
24 .push(if from_y < to_y {
25 from_y + 1..*to_y
26 } else {
27 to_y + 1..*from_y
28 });
29 } else {
30 horizontal_edges
31 .entry(*from_y)
32 .or_default()
33 .push(if from_x < to_x {
34 from_x + 1..*to_x
35 } else {
36 to_x + 1..*from_x
37 });
38 }
39 }
40
41 let largest_area = coords
42 .into_iter()
43 .tuple_combinations()
44 .filter(|&((from_x, from_y), (to_x, to_y))| {
45 let x_range = match from_x.cmp(&to_x) {
46 Ordering::Equal => from_x..from_x + 1,
47 Ordering::Less => from_x + 1..to_x,
48 Ordering::Greater => to_x + 1..from_x,
49 };
50 let y_range = match from_y.cmp(&to_y) {
51 Ordering::Equal => from_y..from_y + 1,
52 Ordering::Less => from_y + 1..to_y,
53 Ordering::Greater => to_y + 1..from_y,
54 };
55 vertical_edges.range(x_range.clone()).all(|(_, columns)| {
56 columns
57 .iter()
58 .all(|column| column.start > y_range.end || column.end < y_range.start)
59 }) && horizontal_edges.range(y_range).all(|(_, rows)| {
60 rows.iter()
61 .all(|row| row.start > x_range.end || row.end < x_range.start)
62 })
63 })
64 .map(|((from_x, from_y), (to_x, to_y))| {
65 (from_x.abs_diff(to_x) + 1) * (from_y.abs_diff(to_y) + 1)
66 })
67 .max()
68 .expect("input file was empty");
69
70 println!("The largest rectangular area you can make is {largest_area}!");
71}