|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use aoclp::num::Zero; |
| 4 | +use aoclp::positioning::pt_3d::{euclidian, Pt3d}; |
| 5 | +use aoclp::solvers_impl::input::safe_get_input_as_many; |
| 6 | +use itertools::Itertools; |
| 7 | + |
| 8 | +pub fn part_1() -> usize { |
| 9 | + circuits(false) |
| 10 | + .1 |
| 11 | + .values() |
| 12 | + .sorted_unstable_by(|a, b| a.cmp(b).reverse()) |
| 13 | + .take(3) |
| 14 | + .product() |
| 15 | +} |
| 16 | + |
| 17 | +pub fn part_2() -> i64 { |
| 18 | + let (_, _, (a, b)) = circuits(true); |
| 19 | + a.x * b.x |
| 20 | +} |
| 21 | + |
| 22 | +fn circuits(all: bool) -> (HashMap<Pt3d, usize>, HashMap<usize, usize>, (Pt3d, Pt3d)) { |
| 23 | + let boxes = input(); |
| 24 | + |
| 25 | + let mut circuit_id = 0usize; |
| 26 | + let mut circuits = HashMap::new(); |
| 27 | + let mut circuit_sizes = HashMap::new(); |
| 28 | + let mut last_pair = (Pt3d::zero(), Pt3d::zero()); |
| 29 | + boxes |
| 30 | + .into_iter() |
| 31 | + .array_combinations() |
| 32 | + .filter(|&[a, b]| a != b) |
| 33 | + .map(|[a, b]| (a, b, euclidian(a, b))) |
| 34 | + .sorted_unstable_by(|(_, _, a), (_, _, b)| a.partial_cmp(b).unwrap()) |
| 35 | + .take(if all { 1_000_000 } else { 1_000 }) |
| 36 | + .for_each(|(a, b, _)| match (circuits.get(&a).copied(), circuits.get(&b).copied()) { |
| 37 | + (Some(a_id), Some(b_id)) if a_id == b_id => (), |
| 38 | + (Some(a_id), Some(b_id)) => { |
| 39 | + circuits.iter_mut().for_each(|(_, id)| { |
| 40 | + if *id == b_id { |
| 41 | + *id = a_id; |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + let b_size = circuit_sizes.remove(&b_id).unwrap(); |
| 46 | + *circuit_sizes.get_mut(&a_id).unwrap() += b_size; |
| 47 | + |
| 48 | + last_pair = (a, b); |
| 49 | + }, |
| 50 | + (Some(a_id), None) => { |
| 51 | + circuits.insert(b, a_id); |
| 52 | + *circuit_sizes.get_mut(&a_id).unwrap() += 1; |
| 53 | + }, |
| 54 | + (None, Some(b_id)) => { |
| 55 | + circuits.insert(a, b_id); |
| 56 | + *circuit_sizes.get_mut(&b_id).unwrap() += 1; |
| 57 | + }, |
| 58 | + (None, None) => { |
| 59 | + circuits.insert(a, circuit_id); |
| 60 | + circuits.insert(b, circuit_id); |
| 61 | + circuit_sizes.insert(circuit_id, 2usize); |
| 62 | + circuit_id += 1; |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + (circuits, circuit_sizes, last_pair) |
| 67 | +} |
| 68 | + |
| 69 | +fn input() -> Vec<Pt3d> { |
| 70 | + safe_get_input_as_many(2025, 8) |
| 71 | +} |
0 commit comments