Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Day 8

Ben C 45589225 62c99d3e

+152 -5
+110 -5
years/2025/src/day_8.rs
··· 1 + use std::{collections::HashMap, ops::Sub}; 1 2 2 3 use advent_core::{Day, day_stuff, ex_for_day}; 3 4 4 5 pub struct Day8; 5 6 7 + #[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)] 8 + pub struct Pos(isize, isize, isize); 9 + 10 + impl Sub for Pos { 11 + type Output = isize; 12 + 13 + fn sub(self, rhs: Self) -> Self::Output { 14 + (self.0 - rhs.0).pow(2) + (self.1 - rhs.1).pow(2) + (self.2 - rhs.2).pow(2) 15 + } 16 + } 17 + 6 18 impl Day for Day8 { 19 + day_stuff!(8, "40", "25272", (usize, Vec<Pos>)); 7 20 8 - day_stuff!(8, "", ""); 21 + fn part_1((amnt, input): Self::Input) -> Option<String> { 22 + let mut circuits = input 23 + .iter() 24 + .enumerate() 25 + .map(|(i, &p)| (p, i)) 26 + .collect::<HashMap<Pos, usize>>(); 27 + 28 + let mut distances = input 29 + .iter() 30 + .enumerate() 31 + .flat_map(|(i, p)| input.iter().skip(i + 1).map(|&p2| (*p, p2, *p - p2))) 32 + .collect::<Vec<_>>(); 33 + 34 + distances.sort_by_key(|(_, _, x)| *x); 35 + 36 + for (p1, p2, _dist) in distances.into_iter().take(amnt) { 37 + let target_circ = circuits.get(&p1).copied().unwrap(); 38 + let replace_circ = circuits.get(&p2).copied().unwrap(); 39 + 40 + if target_circ == replace_circ { 41 + continue; 42 + } 43 + 44 + for (_p, v) in circuits.iter_mut().filter(|(_, v)| **v == replace_circ) { 45 + *v = target_circ; 46 + } 47 + } 48 + 49 + let mut counts = circuits 50 + .into_iter() 51 + .fold(HashMap::new(), |mut acc, (_, c)| { 52 + *(acc.entry(c).or_insert(0)) += 1; 53 + acc 54 + }) 55 + .into_iter() 56 + .map(|(_, count)| count) 57 + .collect::<Vec<_>>(); 58 + 59 + counts.sort_by(|a, b| a.cmp(b).reverse()); 60 + 61 + Some( 62 + counts 63 + .into_iter() 64 + .take(3) 65 + .fold(1, |acc, x| acc * x) 66 + .to_string(), 67 + ) 68 + } 69 + 70 + fn part_2((_amnt, input): Self::Input) -> Option<String> { 71 + let mut circuits = input 72 + .iter() 73 + .enumerate() 74 + .map(|(i, &p)| (p, i)) 75 + .collect::<HashMap<Pos, usize>>(); 76 + 77 + let mut distances = input 78 + .iter() 79 + .enumerate() 80 + .flat_map(|(i, p)| input.iter().skip(i + 1).map(|&p2| (*p, p2, *p - p2))) 81 + .collect::<Vec<_>>(); 82 + 83 + distances.sort_by_key(|(_, _, x)| *x); 84 + 85 + let mut last_2: Option<(Pos, Pos)> = None; 86 + 87 + for (p1, p2, _dist) in distances.into_iter() { 88 + let target_circ = circuits.get(&p1).copied().unwrap(); 89 + let replace_circ = circuits.get(&p2).copied().unwrap(); 90 + 91 + if target_circ == replace_circ { 92 + continue; 93 + } 94 + 95 + last_2 = Some((p1, p2)); 9 96 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 97 + for (_p, v) in circuits.iter_mut().filter(|(_, v)| **v == replace_circ) { 98 + *v = target_circ; 99 + } 100 + } 101 + 102 + let (a, b) = last_2.unwrap(); 103 + 104 + Some((a.0 * b.0).to_string()) 12 105 } 13 106 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 107 + fn parse_input(input: &str) -> Self::Input { 108 + let mut lines = input.lines(); 109 + 110 + let amnt = lines.next().unwrap().parse::<usize>().unwrap(); 111 + 112 + let poses = lines 113 + .map(|l| { 114 + let (x, r) = l.split_once(',').unwrap(); 115 + let (y, z) = r.split_once(',').unwrap(); 116 + Pos(x.parse().unwrap(), y.parse().unwrap(), z.parse().unwrap()) 117 + }) 118 + .collect(); 119 + 120 + (amnt, poses) 16 121 } 17 122 }
+21
years/2025/src/examples/day_8/1.txt
··· 1 + 10 2 + 162,817,812 3 + 57,618,57 4 + 906,360,560 5 + 592,479,940 6 + 352,342,300 7 + 466,668,158 8 + 542,29,236 9 + 431,825,988 10 + 739,650,466 11 + 52,470,668 12 + 216,146,977 13 + 819,987,18 14 + 117,168,530 15 + 805,96,715 16 + 346,949,466 17 + 970,615,88 18 + 941,993,340 19 + 862,61,35 20 + 984,92,344 21 + 425,690,689
+21
years/2025/src/examples/day_8/2.txt
··· 1 + 10 2 + 162,817,812 3 + 57,618,57 4 + 906,360,560 5 + 592,479,940 6 + 352,342,300 7 + 466,668,158 8 + 542,29,236 9 + 431,825,988 10 + 739,650,466 11 + 52,470,668 12 + 216,146,977 13 + 819,987,18 14 + 117,168,530 15 + 805,96,715 16 + 346,949,466 17 + 970,615,88 18 + 941,993,340 19 + 862,61,35 20 + 984,92,344 21 + 425,690,689