|
| 1 | +use std::collections::{HashMap, HashSet}; |
| 2 | + |
| 3 | +use crate::solution::{AocError, Solution}; |
| 4 | + |
| 5 | +fn parse(input: &str) -> Result<HashMap<&str, HashSet<&str>>, AocError> { |
| 6 | + let mut network: HashMap<&str, HashSet<&str>> = HashMap::new(); |
| 7 | + |
| 8 | + for row in input.lines() { |
| 9 | + let (first, second) = row |
| 10 | + .split_once("-") |
| 11 | + .ok_or_else(|| AocError::parse(row, "Invalid network node"))?; |
| 12 | + |
| 13 | + network.entry(first).or_default().insert(second); |
| 14 | + network.entry(second).or_default().insert(first); |
| 15 | + } |
| 16 | + |
| 17 | + Ok(network) |
| 18 | +} |
| 19 | + |
| 20 | +pub struct Day23; |
| 21 | +impl Solution for Day23 { |
| 22 | + type A = u32; |
| 23 | + type B = String; |
| 24 | + |
| 25 | + fn default_input(&self) -> &'static str { |
| 26 | + include_str!("../../../inputs/2024/day23.txt") |
| 27 | + } |
| 28 | + |
| 29 | + fn part_1(&self, input: &str) -> Result<u32, AocError> { |
| 30 | + let network = parse(input)?; |
| 31 | + |
| 32 | + let mut networks_of_three: HashSet<Vec<&str>> = HashSet::new(); |
| 33 | + |
| 34 | + for (first, edges) in network.iter() { |
| 35 | + if first.starts_with("t") { |
| 36 | + for second in edges { |
| 37 | + for third in network[second].intersection(edges) { |
| 38 | + let mut group = vec![*first, *second, *third]; |
| 39 | + group.sort(); |
| 40 | + networks_of_three.insert(group); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + Ok(networks_of_three.len() as u32) |
| 47 | + } |
| 48 | + |
| 49 | + fn part_2(&self, input: &str) -> Result<String, AocError> { |
| 50 | + let network = parse(input)?; |
| 51 | + |
| 52 | + let mut largest = network |
| 53 | + .iter() |
| 54 | + .map(|(current, edges)| { |
| 55 | + let mut interconnected = vec![*current]; |
| 56 | + |
| 57 | + for edge in edges { |
| 58 | + if interconnected |
| 59 | + .iter() |
| 60 | + .all(|node| network[edge].contains(node)) |
| 61 | + { |
| 62 | + interconnected.push(*edge); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + interconnected |
| 67 | + }) |
| 68 | + .max_by(|a, b| a.len().cmp(&b.len())) |
| 69 | + .ok_or_else(|| AocError::logic("No groups found"))?; |
| 70 | + |
| 71 | + largest.sort(); |
| 72 | + let password = largest.join(","); |
| 73 | + |
| 74 | + Ok(password) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod tests { |
| 80 | + use super::*; |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn it_solves_part1_example() { |
| 84 | + assert_eq!( |
| 85 | + Day23.part_1( |
| 86 | + "kh-tc\n\ |
| 87 | + qp-kh\n\ |
| 88 | + de-cg\n\ |
| 89 | + ka-co\n\ |
| 90 | + yn-aq\n\ |
| 91 | + qp-ub\n\ |
| 92 | + cg-tb\n\ |
| 93 | + vc-aq\n\ |
| 94 | + tb-ka\n\ |
| 95 | + wh-tc\n\ |
| 96 | + yn-cg\n\ |
| 97 | + kh-ub\n\ |
| 98 | + ta-co\n\ |
| 99 | + de-co\n\ |
| 100 | + tc-td\n\ |
| 101 | + tb-wq\n\ |
| 102 | + wh-td\n\ |
| 103 | + ta-ka\n\ |
| 104 | + td-qp\n\ |
| 105 | + aq-cg\n\ |
| 106 | + wq-ub\n\ |
| 107 | + ub-vc\n\ |
| 108 | + de-ta\n\ |
| 109 | + wq-aq\n\ |
| 110 | + wq-vc\n\ |
| 111 | + wh-yn\n\ |
| 112 | + ka-de\n\ |
| 113 | + kh-ta\n\ |
| 114 | + co-tc\n\ |
| 115 | + wh-qp\n\ |
| 116 | + tb-vc\n\ |
| 117 | + td-yn" |
| 118 | + ), |
| 119 | + Ok(7) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn it_solves_part2_example() { |
| 125 | + assert_eq!( |
| 126 | + Day23.part_2( |
| 127 | + "kh-tc\n\ |
| 128 | + qp-kh\n\ |
| 129 | + de-cg\n\ |
| 130 | + ka-co\n\ |
| 131 | + yn-aq\n\ |
| 132 | + qp-ub\n\ |
| 133 | + cg-tb\n\ |
| 134 | + vc-aq\n\ |
| 135 | + tb-ka\n\ |
| 136 | + wh-tc\n\ |
| 137 | + yn-cg\n\ |
| 138 | + kh-ub\n\ |
| 139 | + ta-co\n\ |
| 140 | + de-co\n\ |
| 141 | + tc-td\n\ |
| 142 | + tb-wq\n\ |
| 143 | + wh-td\n\ |
| 144 | + ta-ka\n\ |
| 145 | + td-qp\n\ |
| 146 | + aq-cg\n\ |
| 147 | + wq-ub\n\ |
| 148 | + ub-vc\n\ |
| 149 | + de-ta\n\ |
| 150 | + wq-aq\n\ |
| 151 | + wq-vc\n\ |
| 152 | + wh-yn\n\ |
| 153 | + ka-de\n\ |
| 154 | + kh-ta\n\ |
| 155 | + co-tc\n\ |
| 156 | + wh-qp\n\ |
| 157 | + tb-vc\n\ |
| 158 | + td-yn" |
| 159 | + ), |
| 160 | + Ok(String::from("co,de,ka,ta")) |
| 161 | + ); |
| 162 | + } |
| 163 | +} |
0 commit comments