|
| 1 | +use std::{ |
| 2 | + collections::{HashMap, HashSet}, |
| 3 | + mem, |
| 4 | +}; |
| 5 | + |
| 6 | +use aoc_utils::*; |
| 7 | +use itertools::Itertools; |
| 8 | + |
| 9 | +advent_of_code::solution!(8); |
| 10 | + |
| 11 | +fn handle_conn( |
| 12 | + p1: (u64, u64, u64), |
| 13 | + p2: (u64, u64, u64), |
| 14 | + circuits: &mut Vec<HashSet<(u64, u64, u64)>>, |
| 15 | + point_to_circuit: &mut HashMap<(u64, u64, u64), usize>, |
| 16 | +) -> usize { |
| 17 | + let first_p2c = point_to_circuit.get(&p1); |
| 18 | + let second_p2c = point_to_circuit.get(&p2); |
| 19 | + let mut check_circ = 0; |
| 20 | + match (first_p2c, second_p2c) { |
| 21 | + (None, None) => { |
| 22 | + let circuit_id = circuits.len() - 1; |
| 23 | + circuits[circuit_id].insert(p1); |
| 24 | + circuits[circuit_id].insert(p2); |
| 25 | + point_to_circuit.insert(p1, circuit_id); |
| 26 | + point_to_circuit.insert(p2, circuit_id); |
| 27 | + circuits.push(HashSet::new()); |
| 28 | + } |
| 29 | + (None, Some(b)) => { |
| 30 | + let b = *b; |
| 31 | + circuits[b].insert(p1); |
| 32 | + point_to_circuit.insert(p1, b); |
| 33 | + check_circ = b; |
| 34 | + } |
| 35 | + (Some(a), None) => { |
| 36 | + let a = *a; |
| 37 | + circuits[a].insert(p2); |
| 38 | + point_to_circuit.insert(p2, a); |
| 39 | + check_circ = a; |
| 40 | + } |
| 41 | + (Some(a), Some(b)) => { |
| 42 | + let a = a.to_owned(); |
| 43 | + let b = b.to_owned(); |
| 44 | + if a != b { |
| 45 | + // merge curcuits |
| 46 | + let mut circ_to_merge = HashSet::new(); |
| 47 | + mem::swap(&mut circ_to_merge, &mut circuits[b]); |
| 48 | + for p in circ_to_merge.into_iter() { |
| 49 | + circuits[a].insert(p); |
| 50 | + point_to_circuit.insert(p, a); |
| 51 | + } |
| 52 | + } |
| 53 | + check_circ = a |
| 54 | + } |
| 55 | + } |
| 56 | + check_circ |
| 57 | +} |
| 58 | + |
| 59 | +fn distance(a: (u64, u64, u64), b: (u64, u64, u64)) -> u64 { |
| 60 | + // hopefully manhattan distance works |
| 61 | + let x = a.0.abs_diff(b.0).pow(2); |
| 62 | + let y = a.1.abs_diff(b.1).pow(2); |
| 63 | + let z = a.2.abs_diff(b.2).pow(2); |
| 64 | + (x + y + z).isqrt() |
| 65 | +} |
| 66 | + |
| 67 | +pub fn part_one(input: &str) -> Option<u64> { |
| 68 | + let points = input.mlines(|s| { |
| 69 | + s.split(",") |
| 70 | + .map(|n| n.parse::<u64>().expect("Should get int")) |
| 71 | + .collect_tuple::<(u64, u64, u64)>() |
| 72 | + .expect("Should get 3d point") |
| 73 | + }); |
| 74 | + let total_points = points.len(); |
| 75 | + |
| 76 | + let mut circuits = Vec::<HashSet<(u64, u64, u64)>>::with_capacity(total_points); |
| 77 | + circuits.push(HashSet::new()); |
| 78 | + let mut point_to_circuit = HashMap::<(u64, u64, u64), usize>::new(); |
| 79 | + |
| 80 | + #[cfg(test)] |
| 81 | + let num_conns = 10; |
| 82 | + #[cfg(not(test))] |
| 83 | + let num_conns = 1000; |
| 84 | + let conns = points |
| 85 | + .into_iter() |
| 86 | + .tuple_combinations() |
| 87 | + .sorted_by_key(|(a, b)| distance(*a, *b)) |
| 88 | + .take(num_conns); |
| 89 | + for c in conns { |
| 90 | + let _ = handle_conn(c.0, c.1, &mut circuits, &mut point_to_circuit); |
| 91 | + } |
| 92 | + Some( |
| 93 | + circuits |
| 94 | + .iter() |
| 95 | + .map(|s| s.len()) |
| 96 | + .sorted() |
| 97 | + .rev() |
| 98 | + .take(3) |
| 99 | + .product::<usize>() as u64, |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +pub fn part_two(input: &str) -> Option<u64> { |
| 104 | + let points = input.mlines(|s| { |
| 105 | + s.split(",") |
| 106 | + .map(|n| n.parse::<u64>().expect("Should get int")) |
| 107 | + .collect_tuple::<(u64, u64, u64)>() |
| 108 | + .expect("Should get 3d point") |
| 109 | + }); |
| 110 | + let total_points = points.len(); |
| 111 | + |
| 112 | + let mut circuits = Vec::<HashSet<(u64, u64, u64)>>::with_capacity(total_points); |
| 113 | + circuits.push(HashSet::new()); |
| 114 | + let mut point_to_circuit = HashMap::<(u64, u64, u64), usize>::new(); |
| 115 | + |
| 116 | + let conns = points |
| 117 | + .into_iter() |
| 118 | + .tuple_combinations() |
| 119 | + .sorted_by_key(|(a, b)| distance(*a, *b)); |
| 120 | + for c in conns { |
| 121 | + let check_circ = handle_conn(c.0, c.1, &mut circuits, &mut point_to_circuit); |
| 122 | + if circuits[check_circ].len() == total_points { |
| 123 | + return Some(c.0 .0 * c.1 .0); |
| 124 | + } |
| 125 | + } |
| 126 | + None |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(test)] |
| 130 | +mod tests { |
| 131 | + use super::*; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_part_one() { |
| 135 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 136 | + assert_eq!(result, Some(40)); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_part_two() { |
| 141 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 142 | + assert_eq!(result, Some(25272)); |
| 143 | + } |
| 144 | +} |
0 commit comments