-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.sh
62 lines (51 loc) · 1.38 KB
/
10.sh
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
#!/bin/bash
g=()
while read -r line; do
g+=("$line")
done
p1=0
p2=0
DIR=("-1,0" "0,1" "1,0" "0,-1")
R=${#g[@]}
C=${#g[0]}
for (( r = 0; r < R; r++ )); do
#echo "$r - ${g[r]}"
for (( c = 0; c < C; c++ ));do
if [[ ${g[r]:c:1} != "0" ]];then
continue
fi
#echo "$r,$c/ ${g[r]:c:1}"
unset SEEN
unset D
declare -A SEEN # set
declare -A D # dict
D["$r,$c"]=1
SEEN["$r,$c"]=1
Q=("$r,$c") # deque
while [[ ${#Q[@]} > 0 ]];do
begin=${Q[0]}
Q=("${Q[@]:1}") # popleft
IFS=',' read -r sr sc <<< "$begin"
prev=${g[sr]:sc:1}
if [[ $prev == 9 ]]; then
p1=$((p1 + 1))
p2=$((p2 + D["$sr,$sc"]))
fi
for dir in "${DIR[@]}";do
IFS=',' read -r dr dc <<< "${dir}"
rr=$((sr + dr))
cc=$((sc + dc))
if (( -1<rr && rr<R && -1<cc && cc<C )) &&\
[[ ${g[rr]:cc:1} -eq $((prev + 1)) ]]; then
if [[ -z ${SEEN["$rr,$cc"]} ]]; then
SEEN["$rr,$cc"]=1
Q+=("$rr,$cc")
fi
D["$rr,$cc"]=$(( D["$rr,$cc"] + D["$sr,$sc"] ))
fi
done
done
done
done
echo "part 1: $p1"
echo "part 2: $p2"