From 45589225f35c5fa8ac04705cafda535c04b4aaf2 Mon Sep 17 00:00:00 2001 From: Ben C Date: Mon, 8 Dec 2025 00:55:32 -0500 Subject: [PATCH] Day 8 --- years/2025/src/day_8.rs | 115 ++++++++++++++++++++++++++-- years/2025/src/examples/day_8/1.txt | 21 +++++ years/2025/src/examples/day_8/2.txt | 21 +++++ 3 files changed, 152 insertions(+), 5 deletions(-) diff --git a/years/2025/src/day_8.rs b/years/2025/src/day_8.rs index daae1dc..e68aea9 100644 --- a/years/2025/src/day_8.rs +++ b/years/2025/src/day_8.rs @@ -1,17 +1,122 @@ +use std::{collections::HashMap, ops::Sub}; use advent_core::{Day, day_stuff, ex_for_day}; pub struct Day8; +#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)] +pub struct Pos(isize, isize, isize); + +impl Sub for Pos { + type Output = isize; + + fn sub(self, rhs: Self) -> Self::Output { + (self.0 - rhs.0).pow(2) + (self.1 - rhs.1).pow(2) + (self.2 - rhs.2).pow(2) + } +} + impl Day for Day8 { + day_stuff!(8, "40", "25272", (usize, Vec)); + + fn part_1((amnt, input): Self::Input) -> Option { + let mut circuits = input + .iter() + .enumerate() + .map(|(i, &p)| (p, i)) + .collect::>(); + + let mut distances = input + .iter() + .enumerate() + .flat_map(|(i, p)| input.iter().skip(i + 1).map(|&p2| (*p, p2, *p - p2))) + .collect::>(); + + distances.sort_by_key(|(_, _, x)| *x); + + for (p1, p2, _dist) in distances.into_iter().take(amnt) { + let target_circ = circuits.get(&p1).copied().unwrap(); + let replace_circ = circuits.get(&p2).copied().unwrap(); + + if target_circ == replace_circ { + continue; + } + + for (_p, v) in circuits.iter_mut().filter(|(_, v)| **v == replace_circ) { + *v = target_circ; + } + } + + let mut counts = circuits + .into_iter() + .fold(HashMap::new(), |mut acc, (_, c)| { + *(acc.entry(c).or_insert(0)) += 1; + acc + }) + .into_iter() + .map(|(_, count)| count) + .collect::>(); + + counts.sort_by(|a, b| a.cmp(b).reverse()); - day_stuff!(8, "", ""); + Some( + counts + .into_iter() + .take(3) + .fold(1, |acc, x| acc * x) + .to_string(), + ) + } + + fn part_2((_amnt, input): Self::Input) -> Option { + let mut circuits = input + .iter() + .enumerate() + .map(|(i, &p)| (p, i)) + .collect::>(); + + let mut distances = input + .iter() + .enumerate() + .flat_map(|(i, p)| input.iter().skip(i + 1).map(|&p2| (*p, p2, *p - p2))) + .collect::>(); + + distances.sort_by_key(|(_, _, x)| *x); + + let mut last_2: Option<(Pos, Pos)> = None; + + for (p1, p2, _dist) in distances.into_iter() { + let target_circ = circuits.get(&p1).copied().unwrap(); + let replace_circ = circuits.get(&p2).copied().unwrap(); + + if target_circ == replace_circ { + continue; + } - fn part_1(_input: Self::Input) -> Option { - None + last_2 = Some((p1, p2)); + + for (_p, v) in circuits.iter_mut().filter(|(_, v)| **v == replace_circ) { + *v = target_circ; + } + } + + let (a, b) = last_2.unwrap(); + + Some((a.0 * b.0).to_string()) } - fn part_2(_input: Self::Input) -> Option { - None + fn parse_input(input: &str) -> Self::Input { + let mut lines = input.lines(); + + let amnt = lines.next().unwrap().parse::().unwrap(); + + let poses = lines + .map(|l| { + let (x, r) = l.split_once(',').unwrap(); + let (y, z) = r.split_once(',').unwrap(); + Pos(x.parse().unwrap(), y.parse().unwrap(), z.parse().unwrap()) + }) + .collect(); + + (amnt, poses) } } diff --git a/years/2025/src/examples/day_8/1.txt b/years/2025/src/examples/day_8/1.txt index e69de29..907ced5 100644 --- a/years/2025/src/examples/day_8/1.txt +++ b/years/2025/src/examples/day_8/1.txt @@ -0,0 +1,21 @@ +10 +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 diff --git a/years/2025/src/examples/day_8/2.txt b/years/2025/src/examples/day_8/2.txt index e69de29..907ced5 100644 --- a/years/2025/src/examples/day_8/2.txt +++ b/years/2025/src/examples/day_8/2.txt @@ -0,0 +1,21 @@ +10 +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 -- 2.51.2