|
| 1 | +use std::collections::{HashMap, VecDeque}; |
| 2 | + |
| 3 | +pub fn part1(contents: &str) -> i64 { |
| 4 | + let graph = parse_input(contents); |
| 5 | + let mut count = 0; |
| 6 | + let mut queue = VecDeque::new(); |
| 7 | + queue.push_back("you"); |
| 8 | + while !queue.is_empty() { |
| 9 | + let current = queue.pop_front().unwrap(); |
| 10 | + if current == "out" { |
| 11 | + count += 1; |
| 12 | + continue; |
| 13 | + } |
| 14 | + if let Some(neighbors) = graph.get(current) { |
| 15 | + for &neighbor in neighbors { |
| 16 | + queue.push_back(neighbor); |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + count |
| 21 | +} |
| 22 | + |
| 23 | +pub fn part2(contents: &str) -> i64 { |
| 24 | + let graph = parse_input(contents); |
| 25 | + |
| 26 | + // Determine which path exists between "dac" and "fft" |
| 27 | + let paths_dac_fft = count_paths(&graph, "dac", "fft", &mut HashMap::new()); |
| 28 | + let paths_fft_dac = count_paths(&graph, "fft", "dac", &mut HashMap::new()); |
| 29 | + |
| 30 | + if paths_dac_fft > 0 { |
| 31 | + // There is a path from "dac" to "fft" |
| 32 | + let paths_srv_dac = count_paths(&graph, "svr", "dac", &mut HashMap::new()); |
| 33 | + let paths_fft_out = count_paths(&graph, "fft", "out", &mut HashMap::new()); |
| 34 | + paths_dac_fft * paths_fft_out * paths_srv_dac |
| 35 | + } else { |
| 36 | + // There is a path from "fft" to "dac" |
| 37 | + let paths_srv_fft = count_paths(&graph, "svr", "fft", &mut HashMap::new()); |
| 38 | + let paths_dac_out = count_paths(&graph, "dac", "out", &mut HashMap::new()); |
| 39 | + paths_fft_dac * paths_dac_out * paths_srv_fft |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn parse_input(contents: &str) -> HashMap<&str, Vec<&str>> { |
| 44 | + let mut graph = HashMap::new(); |
| 45 | + for l in contents.lines() { |
| 46 | + let parts: Vec<&str> = l.split(": ").collect(); |
| 47 | + let device = parts[0]; |
| 48 | + let outputs: Vec<&str> = parts[1].split(' ').collect(); |
| 49 | + graph.insert(device, outputs); |
| 50 | + } |
| 51 | + graph |
| 52 | +} |
| 53 | + |
| 54 | +fn count_paths( |
| 55 | + graph: &HashMap<&str, Vec<&str>>, |
| 56 | + start: &str, |
| 57 | + end: &str, |
| 58 | + memo: &mut HashMap<String, i64>, |
| 59 | +) -> i64 { |
| 60 | + if let Some(&cached) = memo.get(start) { |
| 61 | + return cached; |
| 62 | + } |
| 63 | + |
| 64 | + if start == end { |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + let mut total_paths = 0; |
| 69 | + if let Some(neighbors) = graph.get(start) { |
| 70 | + for &neighbor in neighbors { |
| 71 | + total_paths += count_paths(graph, neighbor, end, memo); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + memo.insert(start.to_string(), total_paths); |
| 76 | + total_paths |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_part1() { |
| 85 | + let input = vec![ |
| 86 | + "aaa: you hhh", |
| 87 | + "you: bbb ccc", |
| 88 | + "bbb: ddd eee", |
| 89 | + "ccc: ddd eee fff", |
| 90 | + "ddd: ggg", |
| 91 | + "eee: out", |
| 92 | + "fff: out", |
| 93 | + "ggg: out", |
| 94 | + "hhh: ccc fff iii", |
| 95 | + "iii: out", |
| 96 | + ]; |
| 97 | + |
| 98 | + assert_eq!(part1(&input.join("\n")), 5); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_part2() { |
| 103 | + let input = vec![ |
| 104 | + "svr: aaa bbb", |
| 105 | + "aaa: fft", |
| 106 | + "fft: ccc", |
| 107 | + "bbb: tty", |
| 108 | + "tty: ccc", |
| 109 | + "ccc: ddd eee", |
| 110 | + "ddd: hub", |
| 111 | + "hub: fff", |
| 112 | + "eee: dac", |
| 113 | + "dac: fff", |
| 114 | + "fff: ggg hhh", |
| 115 | + "ggg: out", |
| 116 | + "hhh: out", |
| 117 | + ]; |
| 118 | + |
| 119 | + assert_eq!(part2(&input.join("\n")), 2); |
| 120 | + } |
| 121 | +} |
0 commit comments