From 9bcbf9f2d94bbd57b071900a951cbf20066a36a1 Mon Sep 17 00:00:00 2001 From: SOFe Date: Sat, 7 Dec 2024 15:12:16 +0800 Subject: [PATCH] d3 jq --- src/all.rs | 2 ++ src/all/d3.jq | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/all/d3.jq diff --git a/src/all.rs b/src/all.rs index 7a492bd..f9950a5 100644 --- a/src/all.rs +++ b/src/all.rs @@ -209,9 +209,11 @@ main! { day 3 { part 1 { "find" => d3::p1_find, + "jq" => jq!("d3.jq", "d3q1"), } part 2 { "find" => d3::p2_find, + "jq" => jq!("d3.jq", "d3q2"), } } } diff --git a/src/all/d3.jq b/src/all/d3.jq new file mode 100644 index 0000000..a6b69be --- /dev/null +++ b/src/all/d3.jq @@ -0,0 +1,67 @@ +def identify_number: + [ + (.[:3] | tonumber? | tostring), + (.[:2] | tonumber? | tostring), + (.[:1] | tonumber? | tostring) + ] | + .[0] +; + +def parse_after_mul(continue_fn): + identify_number as $left_number_str | + if $left_number_str == null then continue_fn else + .[($left_number_str | length):] | + if .[0:1] != "," then continue_fn else + .[1:] | + identify_number as $right_number_str | + if $right_number_str == null then continue_fn else + .[($right_number_str | length):] | + if .[0:1] != ")" then continue_fn else + .[1:] | + ($left_number_str | tonumber) * ($right_number_str | tonumber) + continue_fn + end + end + end + end +; + +# Finds the sum of mul()s in the receiver buffer. +# On failure, discard the substring processed in the current function and restart with the remaining data. +# If a mul() expression is correctly identified, continue with the remaining data and add the result to the current expression. +def d3q1: + index("mul(") as $index | + if $index == null then 0 else + .[$index + 4:] | parse_after_mul(d3q1) + end +; + +# jq doesn't support calling functions defined after, but fortunately we can just pass the function:6 +def d3q2_dont(d3q2_do): + index("do()") as $index | + if $index == null then 0 else + .[$index+4:] | + d3q2_do + end +; + +def d3q2_mul(d3q2_do): + parse_after_mul(d3q2_do) +; + +def d3q2_do: + index("don't()") as $dont_index | + index("mul(") as $mul_index | + if $dont_index != null and $mul_index != null then + if $dont_index < $mul_index then + .[$dont_index+7:] | d3q2_dont(d3q2_do) + else + .[$mul_index+4:] | d3q2_mul(d3q2_do) + end + elif $dont_index != null then + .[$dont_index+7:] | d3q2_dont(d3q2_do) + elif $mul_index != null then + .[$mul_index+4:] | d3q2_mul(d3q2_do) + else 0 end +; + +def d3q2: d3q2_do;