-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6p1.jq
More file actions
executable file
·30 lines (26 loc) · 949 Bytes
/
Copy pathday6p1.jq
File metadata and controls
executable file
·30 lines (26 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env -S jq -Rs -f
def out_of_range(r; c; height; width):
r < 0 or c < 0 or r >= width or r >= height;
def key(r; c):
[r, c] | map(tostring) | join(",");
def count(grid; r; c; $height; $width):
def rec:
. as [$r, $c, $dr, $dc, $positions] |
if out_of_range($r; $c; $height; $width) then
$positions | length
elif grid[$r][$c] == "#" then
[$r - $dr, $c - $dc, $dc, -$dr, $positions] | rec
else
[$r + $dr, $c + $dc, $dr, $dc, $positions + {(key($r; $c)): true}] | rec
end;
[r, c, -1, 0, {}] | rec;
split("\n") | map(select(. | length > 0) | split(""))
| length as $height
| (.[0] | length) as $width
| . as $grid
| first(
foreach range($height) as $r (null; .;
foreach range($width) as $c(null; .; if $grid[$r][$c] == "^" then [$r, $c] else empty end)
)
) | . as [$r, $c] | {} as $positions
| count($grid; $r; $c; $height; $width)