Created
December 30, 2024 17:34
-
-
Save bozdoz/5c291b31c7317c436bb982cda9cb5d7a to your computer and use it in GitHub Desktop.
aoc 24 1/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused)] | |
use std::collections::{ HashMap, HashSet }; | |
#[derive(Debug)] | |
enum Op { | |
AND, | |
XOR, | |
OR | |
} | |
fn main() { | |
let input = "x00: 1 | |
x01: 1 | |
x02: 1 | |
y00: 0 | |
y01: 1 | |
y02: 0 | |
x00 AND y00 -> z00 | |
x01 XOR y01 -> z01 | |
x02 OR y02 -> z02 | |
"; | |
let input = "x00: 1 | |
x01: 0 | |
x02: 1 | |
x03: 1 | |
x04: 0 | |
y00: 1 | |
y01: 1 | |
y02: 1 | |
y03: 1 | |
y04: 1 | |
ntg XOR fgs -> mjb | |
y02 OR x01 -> tnw | |
kwq OR kpj -> z05 | |
x00 OR x03 -> fst | |
tgd XOR rvg -> z01 | |
vdt OR tnw -> bfw | |
bfw AND frj -> z10 | |
ffh OR nrd -> bqk | |
y00 AND y03 -> djm | |
y03 OR y00 -> psh | |
bqk OR frj -> z08 | |
tnw OR fst -> frj | |
gnj AND tgd -> z11 | |
bfw XOR mjb -> z00 | |
x03 OR x00 -> vdt | |
gnj AND wpb -> z02 | |
x04 AND y00 -> kjc | |
djm OR pbm -> qhw | |
nrd AND vdt -> hwm | |
kjc AND fst -> rvg | |
y04 OR y02 -> fgs | |
y01 AND x02 -> pbm | |
ntg OR kjc -> kwq | |
psh XOR fgs -> tgd | |
qhw XOR tgd -> z09 | |
pbm OR djm -> kpj | |
x03 XOR y03 -> ffh | |
x00 XOR y04 -> ntg | |
bfw OR bqk -> z06 | |
nrd XOR fgs -> wpb | |
frj XOR qhw -> z04 | |
bqk OR frj -> z07 | |
y03 OR x01 -> nrd | |
hwm AND bqk -> z03 | |
tgd XOR rvg -> z12 | |
tnw OR pbm -> gnj | |
"; | |
let mut wires: HashMap<&str, u8> = HashMap::new(); | |
let mut gates: HashMap<&str, (&str, &str, Op)> = HashMap::new(); | |
let mut zeds = 0; | |
let (w, g) = input.split_once("\n\n").unwrap(); | |
for w in w.lines() { | |
let (name, number) = w.split_once(": ").unwrap(); | |
wires.insert(name, if number == "1" { 1 } else { 0 }); | |
} | |
println!("wires: {wires:?}"); | |
for g in g.lines() { | |
let arr = g.split(" ").collect::<Vec<_>>(); | |
let wire = arr[4]; | |
if wire.starts_with("z") { | |
zeds += 1; | |
} | |
gates.insert(wire, ( | |
arr[0], | |
arr[2], | |
match arr[1] { | |
"AND" => Op::AND, | |
"XOR" => Op::XOR, | |
"OR" => Op::OR, | |
_ => panic!("What is this?! {}", arr[1]) | |
} | |
)); | |
} | |
println!("gates: {gates:?}"); | |
let mut visited: HashSet<&str> = HashSet::new(); | |
while visited.len() < gates.len() { | |
for (wire, v) in gates.iter() { | |
if visited.contains(wire) { | |
continue; | |
} | |
let (a, b, op) = v; | |
let a = wires.get(a); | |
let b = wires.get(b); | |
if let Some(a) = a { | |
if let Some(b) = b { | |
wires.insert(wire, match op { | |
Op::AND => a&b, | |
Op::XOR => a^b, | |
Op::OR => a|b, | |
}); | |
visited.insert(wire); | |
} | |
} | |
} | |
} | |
println!("wires: {wires:?}"); | |
let mut ans: usize = 0; | |
println!("zeds: {zeds}"); | |
while zeds >= 0 { | |
zeds -= 1; | |
// leading zeroes | |
let val = format!("z{:0>2}", zeds); | |
if let Some(v) = wires.get(val.as_str()) { | |
println!("{val} {v}"); | |
ans <<= 1; | |
ans |= *v as usize; | |
} | |
} | |
println!("{ans:b} = {ans}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment