Skip to content

Commit

Permalink
add d1q2/jq/hash
Browse files Browse the repository at this point in the history
  • Loading branch information
SOF3 committed Dec 1, 2024
1 parent ec731f8 commit 3777c51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ macro_rules! main {
}
}

#[allow(dead_code)]
pub fn bench(c: &mut Criterion) {
fn try_unwrap<R, E: fmt::Debug>(f: impl FnOnce() -> Result<R, E>) -> R {
f().unwrap()
Expand Down Expand Up @@ -103,6 +104,7 @@ fn call<In: Parse, Out: fmt::Display>(mut f: impl FnMut(In) -> Out, input: &str)
output.to_string()
}

#[allow(dead_code)]
fn call_benched<In: Parse, Out: fmt::Display>(b: &mut Bencher, day: u32, f: impl FnMut(In) -> Out) {
let input = load_input(Mode::Private, day).unwrap();
let parsed: In = Parse::parse(&input);
Expand Down Expand Up @@ -139,6 +141,7 @@ main! {
"sorted" => d1::p2_sorted,
"count" => d1::p2_count,
"bitvec" => d1::p2_bitvec,
"jq/hash" => jq!("d1.jq", "d1q2_hash"),
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/all/d1.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

0 comments on commit 3777c51

Please sign in to comment.