|
| 1 | +#![feature(test)] |
| 2 | +#![expect(unstable_name_collisions)] |
| 3 | + |
| 4 | +use itertools::Itertools; |
| 5 | +use rustc_hash::{FxHashMap, FxHashSet}; |
| 6 | + |
| 7 | +#[derive(Debug)] |
| 8 | +struct Input { |
| 9 | + values: FxHashMap<String, bool>, |
| 10 | + gates: FxHashMap<String, (Gate, String, String)>, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 14 | +enum Gate { |
| 15 | + And, |
| 16 | + Or, |
| 17 | + Xor, |
| 18 | +} |
| 19 | + |
| 20 | +fn setup(input: &str) -> Input { |
| 21 | + let mut lines = input.lines(); |
| 22 | + let values = lines |
| 23 | + .by_ref() |
| 24 | + .take_while(|l| !l.trim().is_empty()) |
| 25 | + .map(|l| { |
| 26 | + let mut s = l.split(": "); |
| 27 | + let name = s.next().unwrap().into(); |
| 28 | + let value = s.next().unwrap().trim() == "1"; |
| 29 | + (name, value) |
| 30 | + }) |
| 31 | + .collect(); |
| 32 | + |
| 33 | + let gates = lines |
| 34 | + .map(|l| { |
| 35 | + let mut s = l.split_whitespace(); |
| 36 | + let a = s.next().unwrap().into(); |
| 37 | + let gate = match s.next().unwrap() { |
| 38 | + "AND" => Gate::And, |
| 39 | + "OR" => Gate::Or, |
| 40 | + "XOR" => Gate::Xor, |
| 41 | + _ => panic!(), |
| 42 | + }; |
| 43 | + let b = s.next().unwrap().into(); |
| 44 | + s.next(); |
| 45 | + let c = s.next().unwrap().into(); |
| 46 | + (c, (gate, a, b)) |
| 47 | + }) |
| 48 | + .collect(); |
| 49 | + |
| 50 | + Input { values, gates } |
| 51 | +} |
| 52 | + |
| 53 | +fn part1(input: &Input) -> usize { |
| 54 | + let mut forward = FxHashMap::<_, Vec<_>>::default(); |
| 55 | + let mut unknown = input |
| 56 | + .gates |
| 57 | + .iter() |
| 58 | + .map(|(c, (_, a, b))| { |
| 59 | + forward.entry(a).or_default().push(c); |
| 60 | + forward.entry(b).or_default().push(c); |
| 61 | + let dependencies = [a, b] |
| 62 | + .into_iter() |
| 63 | + .filter(|&x| !input.values.contains_key(x)) |
| 64 | + .collect::<FxHashSet<_>>(); |
| 65 | + (c, dependencies) |
| 66 | + }) |
| 67 | + .filter(|(_, v)| !v.is_empty()) |
| 68 | + .collect::<FxHashMap<_, _>>(); |
| 69 | + let mut values = input |
| 70 | + .values |
| 71 | + .iter() |
| 72 | + .map(|(p, &v)| (p, v)) |
| 73 | + .collect::<FxHashMap<_, _>>(); |
| 74 | + let mut stack = input |
| 75 | + .gates |
| 76 | + .keys() |
| 77 | + .filter(|&p| !unknown.contains_key(p)) |
| 78 | + .collect::<Vec<_>>(); |
| 79 | + while let Some(p) = stack.pop() { |
| 80 | + let (gate, ref a, ref b) = input.gates[p]; |
| 81 | + let a = values[a]; |
| 82 | + let b = values[b]; |
| 83 | + let c = match gate { |
| 84 | + Gate::And => a & b, |
| 85 | + Gate::Or => a | b, |
| 86 | + Gate::Xor => a ^ b, |
| 87 | + }; |
| 88 | + values.insert(p, c); |
| 89 | + |
| 90 | + for &q in forward.get(p).into_iter().flatten() { |
| 91 | + if unknown.contains_key(q) { |
| 92 | + unknown.get_mut(q).unwrap().remove(p); |
| 93 | + if unknown[q].is_empty() { |
| 94 | + unknown.remove(q); |
| 95 | + stack.push(q); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + values |
| 102 | + .keys() |
| 103 | + .filter(|k| k.starts_with('z')) |
| 104 | + .sorted_unstable() |
| 105 | + .rev() |
| 106 | + .fold(0, |acc, k| acc << 1 | (values[k] as usize)) |
| 107 | +} |
| 108 | + |
| 109 | +fn part2(input: &Input) -> String { |
| 110 | + let last_z = input |
| 111 | + .gates |
| 112 | + .keys() |
| 113 | + .filter(|k| k.starts_with('z')) |
| 114 | + .max() |
| 115 | + .unwrap(); |
| 116 | + let mut forward = FxHashMap::<_, Vec<_>>::default(); |
| 117 | + for (c, (_, a, b)) in &input.gates { |
| 118 | + forward.entry(a).or_default().push(c); |
| 119 | + forward.entry(b).or_default().push(c); |
| 120 | + } |
| 121 | + input |
| 122 | + .gates |
| 123 | + .keys() |
| 124 | + .filter(|&g| { |
| 125 | + let (gate, ref a, ref b) = input.gates[g]; |
| 126 | + gate != Gate::Xor && g.starts_with('z') && g != last_z |
| 127 | + || gate == Gate::Xor |
| 128 | + && forward |
| 129 | + .get(g) |
| 130 | + .into_iter() |
| 131 | + .flatten() |
| 132 | + .any(|&x| input.gates[x].0 == Gate::Or) |
| 133 | + || gate == Gate::And |
| 134 | + && a != "x00" |
| 135 | + && b != "x00" |
| 136 | + && forward |
| 137 | + .get(g) |
| 138 | + .into_iter() |
| 139 | + .flatten() |
| 140 | + .any(|&x| input.gates[x].0 != Gate::Or) |
| 141 | + || gate == Gate::Xor |
| 142 | + && !g.starts_with('z') |
| 143 | + && [a, b] |
| 144 | + .into_iter() |
| 145 | + .any(|x| input.gates.get(x).is_some_and(|x| x.0 == Gate::Xor)) |
| 146 | + }) |
| 147 | + .sorted_unstable() |
| 148 | + .map(|s| s.as_str()) |
| 149 | + .intersperse(",") |
| 150 | + .collect() |
| 151 | +} |
| 152 | + |
| 153 | +aoc::main!(2024, 24, ex: 1[a], 2[a]); |
0 commit comments