-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21p1.jq
More file actions
executable file
·69 lines (64 loc) · 2.01 KB
/
Copy pathday21p1.jq
File metadata and controls
executable file
·69 lines (64 loc) · 2.01 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env -S jqc -Rs -f
# greedy solution is okay since we always return to the A button
{
"7": [0, 0], "8": [0, 1], "9": [0, 2],
"4": [1, 0], "5": [1, 1], "6": [1, 2],
"1": [2, 0], "2": [2, 1], "3": [2, 2],
" ": [3, 0], "0": [3, 1], "A": [3, 2]
} as $NUMPAD
| {
" ": [0, 0], "^": [0, 1], "A": [0, 2],
"<": [1, 0], "v": [1, 1], ">": [1, 2]
} as $DPAD
| def find_path($y; $x; $ch; $map):
# given that the robot is currently on $y, $x, find a shortest path to $ch
$map[$ch] as [$y2, $x2]
| ("v" * ($y2 - $y)) as $down
| ("^" * ($y - $y2)) as $up
| (">" * ($x2 - $x)) as $right
| ("<" * ($x - $x2)) as $left
| if $map[" "][0] == $y and $map[" "][1] == $x2 then
# start with y
$down + $up + $right + $left
elif $map[" "][0] == $y2 and $map[" "][1] == $x then
# start with x
$right + $left + $down + $up
else
# by distance from A
$left + $down + $up + $right
end | split("") + ["A"];
def solve_for($goal; $map):
# goal index, path taken, cur_loc
[0, [], $map["A"]] | until(.[0] == ($goal | length);
.[0] as $i
| .[2] as [$y, $x]
| $goal[$i] as $ch
| .[1] | [$i + 1, . + find_path($y; $x; $ch; $map), $map[$ch]]
) | .[1];
def get_child($path; $map):
# for debugging, not particularly efficient
[foreach $path[] as $ch ($map["A"];
. as [$y, $x]
| if $ch == "<" then
[$y, $x - 1]
elif $ch == ">" then
[$y, $x + 1]
elif $ch == "^" then
[$y - 1, $x]
elif $ch == "v" then
[$y + 1, $x]
end;
if $ch == "A" then
. as $val | $map | to_entries | map(select(.value == $val) | .key) | .[0]
else
empty
end
)];
split("\n") | map(select(length > 0))
| reduce .[] as $line (0;
. as $total
| solve_for(($line | split("")); $NUMPAD)
| solve_for(.; $DPAD)
| solve_for(.; $DPAD) | length
| . * ($line | scan("\\d+") | tonumber) + $total
)