From 3777c5190782e3ae79cf93d5d607bd39e46d65c6 Mon Sep 17 00:00:00 2001 From: SOFe Date: Sun, 1 Dec 2024 19:05:23 +0800 Subject: [PATCH] add d1q2/jq/hash --- src/all.rs | 3 +++ src/all/d1.jq | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/all.rs b/src/all.rs index 8586802..8517ce1 100644 --- a/src/all.rs +++ b/src/all.rs @@ -41,6 +41,7 @@ macro_rules! main { } } + #[allow(dead_code)] pub fn bench(c: &mut Criterion) { fn try_unwrap(f: impl FnOnce() -> Result) -> R { f().unwrap() @@ -103,6 +104,7 @@ fn call(mut f: impl FnMut(In) -> Out, input: &str) output.to_string() } +#[allow(dead_code)] fn call_benched(b: &mut Bencher, day: u32, f: impl FnMut(In) -> Out) { let input = load_input(Mode::Private, day).unwrap(); let parsed: In = Parse::parse(&input); @@ -139,6 +141,7 @@ main! { "sorted" => d1::p2_sorted, "count" => d1::p2_count, "bitvec" => d1::p2_bitvec, + "jq/hash" => jq!("d1.jq", "d1q2_hash"), } } } diff --git a/src/all/d1.jq b/src/all/d1.jq index 2b0658a..70620c2 100644 --- a/src/all/d1.jq +++ b/src/all/d1.jq @@ -8,3 +8,25 @@ def d1q1: ) | add ; + +def uniq_count: + group_by(.) | + map( + (.[0] | tostring) as $key | + {$key: length} + ) | + add +; + +def d1q2_hash: + split("\n") | + map( + split(" ") | + map(tonumber?) | + select(length == 2) + ) | + (map(.[0]) | uniq_count) as $left | + (map(.[1]) | uniq_count) as $right | + $left | with_entries(.value *= ($right[.key | tostring] // 0) * (.key | tonumber)) | + values | add +;