-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.sh
81 lines (74 loc) · 2.09 KB
/
08.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
#!/bin/bash
g=()
mapfile -t g
printf "%s\n" "${g[@]}"
declare -A A
R=${#g[@]}
C=${#g[0]}
for (( r = 0; r < R; r++ )); do
for (( c = 0; c < C; c++ )); do
char=${g[r]:c:1}
if [[ "$char" != "." ]]; then
A["$char"]+="($r,$c) "
fi
done
done
#for k in "${!A[@]}"; do echo "dbg/ key $k, set: ${A[$k]}";done
declare -A btw
declare -A ALL
for atn in "${!A[@]}"; do
coor="${A["$atn"]}"
#echo; echo "$atn - $coor" # ok/
for tup in $coor; do
tup=${tup//[()]/}
sr=${tup%,*}
sc=${tup#*,}
#echo; echo "sr,sc/ $sr, $sc" # ok/
for cmp in $coor; do
cmp=${cmp//[()]/}
er=${cmp%,*}
ec=${cmp#*,}
if [[ "$sr" == "$er" && "$sc" == "$ec" ]]; then
continue
fi
#echo "er,ec/ $er, $ec" # ok/
dr=$(( er - sr ))
dc=$(( ec - sc ))
# PART 1
rr=$(( sr - dr ))
cc=$(( sc - dc ))
if (( -1 < rr && rr < R && -1 < cc && cc < C )); then
btw["($rr,$cc)"]=1
fi
rr=$(( er + dr ))
cc=$(( ec + dc ))
if (( -1 < rr && rr < R && -1 < cc && cc < C )); then
btw["($rr,$cc)"]=1
fi
# PART 2
t=0
while true; do
changed=false
rr=$(( er + dr * t ))
cc=$(( ec + dc * t ))
if (( -1 < rr && rr < R && -1 < cc && cc < C )); then
ALL["($rr,$cc)"]=1
changed=true
fi
rr=$(( sr - dr * t ))
cc=$(( sc - dc * t ))
if (( -1 < rr && rr < R && -1 < cc && cc < C )); then
ALL["($rr,$cc)"]=1
changed=true
fi
if [[ "$changed" == false ]]; then
break
fi
(( t++ ))
done
done
done
done
#for k in "${!btw[@]}";do echo"dbg/ key $k, set: ${btw[$k]}";done
echo "part 1/ ${#btw[@]}"
echo "part 2/ ${#ALL[@]}"