|
| 1 | +#![allow(non_snake_case)] |
| 2 | + |
| 3 | +use aoc::Puzzle; |
| 4 | +use std::collections::{HashMap, HashSet}; |
| 5 | + |
| 6 | +struct AoC2024_23; |
| 7 | + |
| 8 | +impl AoC2024_23 {} |
| 9 | + |
| 10 | +impl aoc::Puzzle for AoC2024_23 { |
| 11 | + type Input = HashMap<String, HashSet<String>>; |
| 12 | + type Output1 = usize; |
| 13 | + type Output2 = String; |
| 14 | + |
| 15 | + aoc::puzzle_year_day!(2024, 23); |
| 16 | + |
| 17 | + fn parse_input(&self, lines: Vec<String>) -> Self::Input { |
| 18 | + let mut edges: HashMap<String, HashSet<String>> = HashMap::new(); |
| 19 | + lines.iter().for_each(|line| { |
| 20 | + let (node_1, node_2) = line.split_once("-").unwrap(); |
| 21 | + edges |
| 22 | + .entry(String::from(node_1)) |
| 23 | + .or_default() |
| 24 | + .insert(String::from(node_2)); |
| 25 | + edges |
| 26 | + .entry(String::from(node_2)) |
| 27 | + .or_default() |
| 28 | + .insert(String::from(node_1)); |
| 29 | + }); |
| 30 | + edges |
| 31 | + } |
| 32 | + |
| 33 | + fn part_1(&self, edges: &Self::Input) -> Self::Output1 { |
| 34 | + let mut triangles: HashSet<(&str, &str, &str)> = HashSet::new(); |
| 35 | + for t in edges.keys().filter(|node| node.starts_with("t")) { |
| 36 | + for neighbour_1 in edges.get(t).unwrap() { |
| 37 | + for neighbour_2 in edges.get(neighbour_1).unwrap() { |
| 38 | + if edges.get(neighbour_2).unwrap().contains(t) { |
| 39 | + let mut triangle = [t, neighbour_1, neighbour_2]; |
| 40 | + triangle.sort_unstable(); |
| 41 | + triangles.insert(( |
| 42 | + triangle[0], |
| 43 | + triangle[1], |
| 44 | + triangle[2], |
| 45 | + )); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + triangles.len() |
| 51 | + } |
| 52 | + |
| 53 | + fn part_2(&self, edges: &Self::Input) -> Self::Output2 { |
| 54 | + let mut clique = vec![]; |
| 55 | + let mut largest = vec![]; |
| 56 | + let mut seen: HashSet<&String> = HashSet::new(); |
| 57 | + for (n1, neighbours) in edges { |
| 58 | + if seen.contains(n1) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + clique.clear(); |
| 62 | + clique.push(n1); |
| 63 | + for n2 in neighbours { |
| 64 | + let nn = edges.get(n2).unwrap(); |
| 65 | + if clique.iter().all(|&c| nn.contains(c)) { |
| 66 | + seen.insert(n2); |
| 67 | + clique.push(n2); |
| 68 | + } |
| 69 | + } |
| 70 | + if clique.len() > largest.len() { |
| 71 | + largest.clone_from(&clique); |
| 72 | + } |
| 73 | + } |
| 74 | + largest.sort_unstable(); |
| 75 | + largest |
| 76 | + .iter() |
| 77 | + .copied() |
| 78 | + .map(String::as_str) |
| 79 | + .collect::<Vec<_>>() |
| 80 | + .join(",") |
| 81 | + } |
| 82 | + |
| 83 | + fn samples(&self) { |
| 84 | + aoc::puzzle_samples! { |
| 85 | + self, part_1, TEST, 7, |
| 86 | + self, part_2, TEST, "co,de,ka,ta" |
| 87 | + }; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn main() { |
| 92 | + AoC2024_23 {}.run(std::env::args()); |
| 93 | +} |
| 94 | + |
| 95 | +const TEST: &str = "\ |
| 96 | +kh-tc |
| 97 | +qp-kh |
| 98 | +de-cg |
| 99 | +ka-co |
| 100 | +yn-aq |
| 101 | +qp-ub |
| 102 | +cg-tb |
| 103 | +vc-aq |
| 104 | +tb-ka |
| 105 | +wh-tc |
| 106 | +yn-cg |
| 107 | +kh-ub |
| 108 | +ta-co |
| 109 | +de-co |
| 110 | +tc-td |
| 111 | +tb-wq |
| 112 | +wh-td |
| 113 | +ta-ka |
| 114 | +td-qp |
| 115 | +aq-cg |
| 116 | +wq-ub |
| 117 | +ub-vc |
| 118 | +de-ta |
| 119 | +wq-aq |
| 120 | +wq-vc |
| 121 | +wh-yn |
| 122 | +ka-de |
| 123 | +kh-ta |
| 124 | +co-tc |
| 125 | +wh-qp |
| 126 | +tb-vc |
| 127 | +td-yn |
| 128 | +"; |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +mod tests { |
| 132 | + use super::*; |
| 133 | + |
| 134 | + #[test] |
| 135 | + pub fn samples() { |
| 136 | + AoC2024_23 {}.samples(); |
| 137 | + } |
| 138 | +} |
0 commit comments