Skip to content

Commit

Permalink
d7 jq solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
SOF3 committed Dec 8, 2024
1 parent 0948ec1 commit f024627
Show file tree
Hide file tree
Showing 2 changed files with 89 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 @@ -83,9 +83,11 @@ macros::all! {
day 7 {
part 1 {
"reversed" => p1_reversed,
"jq" => jq["d7q1"],
}
part 2 {
"reversed" => p2_reversed,
"jq" => jq["d7q2"],
}
}
day 8 {
Expand Down
87 changes: 87 additions & 0 deletions src/all/d7.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
def parse_line:
split(": ") |
select(length == 2) |
{
result: .[0] | tonumber,
operands: (.[1] | split(" ") | map(tonumber)),
}
;

# equivalent to haskell functions of the same name
def list_init: .[:length - 1];
def list_last: .[length - 1];

def is_valid_by_add(is_valid):
(.result - (.operands | list_last)) as $sub |
if $sub >= 0 then
{
result: $sub,
operands: (.operands | list_init),
} | is_valid
else
false
end
;

def is_valid_by_mul(is_valid):
if .result % (.operands | list_last) == 0 then
{
result: (.result / (.operands | list_last)),
operands: (.operands | list_init),
} | is_valid
else
false
end
;

def is_valid_q1:
if .operands | length == 0 then
.result == 0
else
is_valid_by_add(is_valid_q1) or is_valid_by_mul(is_valid_q1)
end
;

def d7q1:
split("\n") | map(
parse_line |
select(is_valid_q1) |
.result
) |
add
;

def is_valid_by_concat(is_valid):
(.result | tostring) as $result_string |
(.operands | list_last) as $last |
($last | tostring) as $last_string |
if $result_string | endswith($last_string) then
{
result: (
$result_string |
.[:length - ($last_string | length)] |
tonumber
),
operands: (.operands | list_init),
} | is_valid
else
false
end
;

def is_valid_q2:
if .operands | length == 0 then
.result == 0
else
is_valid_by_add(is_valid_q2) or is_valid_by_mul(is_valid_q2) or is_valid_by_concat(is_valid_q2)
end
;

def d7q2:
split("\n") | map(
parse_line |
select(is_valid_q2) |
.result
) |
add
;

0 comments on commit f024627

Please sign in to comment.