|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use itertools::Itertools; |
| 4 | + |
| 5 | +advent_of_code::solution!(11); |
| 6 | + |
| 7 | +fn parse_input(input: &str) -> (HashMap<usize, Vec<usize>>, HashMap<String, usize>) { |
| 8 | + let mut machine_ids = HashMap::new(); |
| 9 | + let s_to_id = move |m: String, machine_ids: &mut HashMap<String, usize>| { |
| 10 | + if let Some(x) = machine_ids.get(&m) { |
| 11 | + *x |
| 12 | + } else { |
| 13 | + let curr = machine_ids.len(); |
| 14 | + machine_ids.insert(m, curr); |
| 15 | + curr |
| 16 | + } |
| 17 | + }; |
| 18 | + let mut machines = HashMap::new(); |
| 19 | + input.lines().for_each(|s| { |
| 20 | + let mut parts = s.split_whitespace(); |
| 21 | + let machine = parts |
| 22 | + .next() |
| 23 | + .expect("Should find machine") |
| 24 | + .trim_end_matches(':') |
| 25 | + .to_string(); |
| 26 | + let m = s_to_id(machine, &mut machine_ids); |
| 27 | + let outputs = parts |
| 28 | + .map(|x| s_to_id(x.to_string(), &mut machine_ids)) |
| 29 | + .collect_vec(); |
| 30 | + machines.insert(m, outputs); |
| 31 | + }); |
| 32 | + (machines, machine_ids) |
| 33 | +} |
| 34 | + |
| 35 | +fn recurse_paths( |
| 36 | + m: usize, |
| 37 | + end: usize, |
| 38 | + machines: &HashMap<usize, Vec<usize>>, |
| 39 | + memo: &mut HashMap<usize, u64>, |
| 40 | +) -> u64 { |
| 41 | + if m == end { |
| 42 | + return 1; |
| 43 | + } |
| 44 | + if let Some(x) = memo.get(&m) { |
| 45 | + return *x; |
| 46 | + } |
| 47 | + let mut total = 0; |
| 48 | + for x in machines.get(&m).expect("All outputs should be inputs") { |
| 49 | + total += recurse_paths(*x, end, machines, memo); |
| 50 | + } |
| 51 | + memo.insert(m, total); |
| 52 | + total |
| 53 | +} |
| 54 | + |
| 55 | +pub fn part_one(input: &str) -> Option<u64> { |
| 56 | + let (machines, machine_ids) = parse_input(input); |
| 57 | + let start = machine_ids.get("you").unwrap(); |
| 58 | + let end = machine_ids.get("out").unwrap(); |
| 59 | + let mut seen = HashMap::new(); |
| 60 | + let total = recurse_paths(*start, *end, &machines, &mut seen); |
| 61 | + Some(total) |
| 62 | +} |
| 63 | + |
| 64 | +fn recurse_paths_part2( |
| 65 | + m: usize, |
| 66 | + end: usize, |
| 67 | + dac: usize, |
| 68 | + fft: usize, |
| 69 | + mut seen_dac: bool, |
| 70 | + mut seen_fft: bool, |
| 71 | + machines: &HashMap<usize, Vec<usize>>, |
| 72 | + memo: &mut HashMap<(usize, bool, bool), u64>, |
| 73 | +) -> u64 { |
| 74 | + if m == end { |
| 75 | + return if seen_dac && seen_fft { 1 } else { 0 }; |
| 76 | + } |
| 77 | + if m == dac { |
| 78 | + seen_dac = true; |
| 79 | + } |
| 80 | + if m == fft { |
| 81 | + seen_fft = true; |
| 82 | + } |
| 83 | + if let Some(x) = memo.get(&(m, seen_dac, seen_fft)) { |
| 84 | + return *x; |
| 85 | + } |
| 86 | + |
| 87 | + let mut total = 0; |
| 88 | + for x in machines.get(&m).expect("All outputs should be inputs") { |
| 89 | + total += recurse_paths_part2(*x, end, dac, fft, seen_dac, seen_fft, machines, memo); |
| 90 | + } |
| 91 | + memo.insert((m, seen_dac, seen_fft), total); |
| 92 | + total |
| 93 | +} |
| 94 | + |
| 95 | +pub fn part_two(input: &str) -> Option<u64> { |
| 96 | + let (machines, machine_ids) = parse_input(input); |
| 97 | + let start = machine_ids.get("svr").unwrap(); |
| 98 | + let end = machine_ids.get("out").unwrap(); |
| 99 | + let dac = machine_ids.get("dac").unwrap(); |
| 100 | + let fft = machine_ids.get("fft").unwrap(); |
| 101 | + let mut seen = HashMap::new(); |
| 102 | + let total = recurse_paths_part2(*start, *end, *dac, *fft, false, false, &machines, &mut seen); |
| 103 | + Some(total) |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use super::*; |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_part_one() { |
| 112 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 113 | + assert_eq!(result, Some(5)); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_part_two() { |
| 118 | + let result = part_two(&advent_of_code::template::read_file_part( |
| 119 | + "examples", DAY, 2, |
| 120 | + )); |
| 121 | + assert_eq!(result, Some(2)); |
| 122 | + } |
| 123 | +} |
0 commit comments