Skip to content

Commit

Permalink
d3 jq
Browse files Browse the repository at this point in the history
  • Loading branch information
SOF3 committed Dec 7, 2024
1 parent bcf3db7 commit 9bcbf9f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
67 changes: 67 additions & 0 deletions src/all/d3.jq
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 9bcbf9f

Please sign in to comment.