-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.sh
100 lines (90 loc) · 2.16 KB
/
06.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
g=()
mapfile -t g
printf "%s\n" "${g[@]}"
R=${#g[@]}
C=${#g[0]}
for (( r = 0; r < R; r++ )); do
#echo "${g[r]}"
for (( c = 0; c < C; c++ )); do
chr=${g[r]:c:1}
if [[ $chr == "^" ]]; then
sr=$r
sc=$c
fi
done
done
echo "start/ $sr,$sc"
declare -a DIR
DIR[0]="-1,0"
DIR[1]="0,1"
DIR[2]="1,0"
DIR[3]="0,-1" # accessing/ dr=$(echo $dir | cut -d ',' -f1)
declare -A SEEN
SEEN["$sr,$sc"]=1 # exists/ if [[ -v SEEN["$nr,$nc"] ]]; then
r=$sr
c=$sc
i=0
while true; do
dr=$(echo ${DIR[i]} | cut -d ',' -f1)
dc=$(echo ${DIR[i]} | cut -d ',' -f2)
((rr = r + dr))
((cc = c + dc))
if ! (( -1 < rr && rr < R && -1 < cc && cc < C )); then
echo "part 1: ${#SEEN[@]}"
break
fi
chr=${g[rr]:cc:1}
if [[ $chr == "#" ]]; then ((i = (i + 1) % 4)) ;
else
(( r = rr ))
(( c = cc ))
if ! [[ -v SEEN["$r,$c"] ]]; then
SEEN["$r,$c"]=1
fi
fi
done
#TODO
# modify one char at (r,c) then change it back
# line="${g[r]}"
# char="${g[r]:c:1}"
# g[r]="${line:0:c}${REPL}${line:c+1}" # MOD
# g[r]="${line:0:c}${char}${line:c+1}" # swap it back
r2=0
for coor in "${!SEEN[@]}"; do
or=$(echo $coor | cut -d ',' -f1)
oc=$(echo $coor | cut -d ',' -f2)
line="${g[or]}"
g[or]="${line:0:oc}#${line:oc+1}"
r=$sr
c=$sc
i=0
unset visited
declare -A visited
visited["$sr,$sc,$i"]=1
while true; do
dr=$(echo ${DIR[i]} | cut -d ',' -f1)
dc=$(echo ${DIR[i]} | cut -d ',' -f2)
((rr = r + dr))
((cc = c + dc))
if ! (( -1 < rr && rr < R && -1 < cc && cc < C )); then
break
fi
chr=${g[rr]:cc:1}
if [[ $chr == "#" ]]; then ((i = (i + 1) % 4)) ;
else
(( r = rr ))
(( c = cc ))
fi
if ! [[ -v visited["$r,$c,$i"] ]]; then
visited["$r,$c,$i"]=1
else
(( r2++ ))
echo "obs/ $or,$oc,$r2" #,${!visited[@]}"
#for (( r = 0; r < R; r++ ));do;echo"${g[r]}";done;echo
break
fi
done
g[or]="${line:0:oc}.${line:oc+1}"
done
echo "part 2: $r2"