From 7078fa47e516697c8c5a4aae0216d919c0c86187 Mon Sep 17 00:00:00 2001 From: javalsai Date: Thu, 11 Dec 2025 15:16:21 +0100 Subject: [PATCH] do p2, not very perf oriented --- 2025/11/p2.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 2025/11/p2.rs diff --git a/2025/11/p2.rs b/2025/11/p2.rs new file mode 100644 index 0000000..d51e4d5 --- /dev/null +++ b/2025/11/p2.rs @@ -0,0 +1,54 @@ +#![feature(slice_split_once)] + +use std::{collections::HashMap, hash::Hash}; + +#[unsafe(no_mangle)] +pub extern "Rust" fn challenge_usize(buf: &[u8]) -> usize { + let conns = buf[..(buf.len() - 1)] + .split(|&b| b == b'\n') + .map(|ln| { + let (left, right) = ln.split_once(|&b| b == b':').unwrap(); + let conns = unsafe { str::from_utf8_unchecked(right) } + .trim() + .split(' ') + .collect::>(); + + (unsafe { str::from_utf8_unchecked(left) }, conns) + }) + .collect::>(); + + let mut hoist = HashMap::new(); + count_paths(&mut hoist, &conns, "svr", false, false) +} + +fn count_paths<'a>( + hoist: &mut HashMap<(&'a str, bool, bool), usize>, + set: &HashMap<&str, Vec<&'a str>>, + from: &'a str, + found_fft: bool, + found_dac: bool, +) -> usize { + let mut count = 0; + if let Some(&hoisted) = hoist.get(&(from, found_fft, found_dac)) { + return hoisted; + }; + + for &subk in set.get(from).unwrap() { + if subk == "out" { + if found_dac && found_fft { + count += 1; + } + } else { + let found_fft = found_fft || subk == "fft"; + let found_dac = found_dac || subk == "dac"; + + let val = count_paths(hoist, set, subk, found_fft, found_dac); + + hoist.insert((subk, found_fft, found_dac), val); + + count += val; + } + } + + count +}