From 0000033060601bb71bff9bad84c8cf4cdc9a09c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Thu, 2 Jan 2025 22:43:58 +0100 Subject: [PATCH] solution: day 24 --- data/examples/24.txt | 47 +++++++++++++++ src/bin/24.rs | 132 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 data/examples/24.txt create mode 100644 src/bin/24.rs diff --git a/data/examples/24.txt b/data/examples/24.txt new file mode 100644 index 000000000..94b6eed --- /dev/null +++ b/data/examples/24.txt @@ -0,0 +1,47 @@ +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 diff --git a/src/bin/24.rs b/src/bin/24.rs new file mode 100644 index 000000000..3721c10 --- /dev/null +++ b/src/bin/24.rs @@ -0,0 +1,132 @@ +use std::collections::VecDeque; + +use hashbrown::HashMap; +use itertools::Itertools; +use regex::Regex; + +#[derive(Debug, Clone, Copy)] +enum Gate { + And, + Or, + XOr, +} + +#[derive(Debug)] +struct LogicGate<'a> { + input_one: &'a str, + input_two: &'a str, + output: &'a str, + gate: Gate, +} + +fn parse_input(input: &str) -> (HashMap<&str, bool>, Vec) { + let (uvals, uevals) = input.split_once("\n\n").unwrap(); + let vals: HashMap<_, _> = uvals + .lines() + .map(|x| x.split_once(": ").unwrap()) + .map(|(k, v)| (k, v == "1")) + .collect(); + + let mut logic_gates = Vec::new(); + let re = Regex::new(r#"((?:\w|\d){3}) (AND|XOR|OR) ((?:\w|\d){3}) -> ((?:\w|\d){3})"#).unwrap(); + for capt in re.captures_iter(uevals) { + let input_one = capt.get(1).unwrap().as_str(); + let gate = match capt.get(2).unwrap().as_str() { + "AND" => Gate::And, + "OR" => Gate::Or, + "XOR" => Gate::XOr, + _ => unreachable!(), + }; + let input_two = capt.get(3).unwrap().as_str(); + let output = capt.get(4).unwrap().as_str(); + logic_gates.push(LogicGate { + input_one, + input_two, + output, + gate, + }) + } + + (vals, logic_gates) +} + +pub fn part_one(input: &str) -> Option { + let (mut vals, logic_gates) = parse_input(input); + let mut queue = VecDeque::from(logic_gates); + + while let Some(s) = queue.pop_front() { + if !vals.contains_key(s.input_one) || !vals.contains_key(s.input_two) { + queue.push_back(s); + continue; + } + + let v1 = vals[s.input_one]; + let v2 = vals[s.input_two]; + + let o = match s.gate { + Gate::And => v1 & v2, + Gate::Or => v1 | v2, + Gate::XOr => v1 ^ v2, + }; + + vals.insert(s.output, o); + } + + let mut zs = vals + .into_iter() + .filter(|x| x.0.starts_with('z')) + .collect_vec(); + zs.sort(); + zs.into_iter() + .enumerate() + .filter(|x| x.1 .1) + .map(|(i, _)| 1 << i) + .sum::() + .into() +} + +pub fn part_two(_input: &str) -> Option { + // Used graphviz for this part + // ```rs + // let (_, logic_gates) = parse_input(input); + // println!("digraph A {{"); + // for (idx, lg) in logic_gates.iter().enumerate() { + // let LogicGate { + // input_one: i1, + // input_two: i2, + // output: o1, + // gate: g, + // } = lg; + // println!("{i1} -> {g:?}_{idx}"); + // println!("{i2} -> {g:?}_{idx}"); + // println!("{g:?}_{idx} -> {o1}"); + // } + // println!("}}"); + // ``` + // + // and then pipe to dot + // ```bash + // ... | dot -Tsvg -Kneato > grpah.svg + // ``` + // + // i looked for errors in the pattern and found below + // solution for my input + + let mut res = ["fkp", "z06", "z11", "ngr", "z31", "mfm", "bpt", "krj"]; + res.sort(); + res.join(",").to_string().into() +} + +aoc::solution!(24); + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + assert_eq!( + part_one(&aoc::template::read_file("examples", 24)), + Some(2024) + ); + } +}