-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3_part2.sh
More file actions
executable file
·59 lines (47 loc) · 1.48 KB
/
day3_part2.sh
File metadata and controls
executable file
·59 lines (47 loc) · 1.48 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
#!/usr/local/bin/bash
declare -A fabric
declare -A suggestions
while read -r line; do
id=$(echo "$line" | sed -e 's/\#\([0-9]*\)\ .*/\1/')
if (( id % 100 == 0 )); then
echo "Parsing row: $id "
fi
position=$(echo "$line" | sed -e 's/.*@\ \([0-9]*,[0-9]*\):\ .*/\1/')
left_pos=${position%,*}
top_pos=${position#*,}
area=$(echo "$line" | sed -e 's/.*:\ \([0-9]*x[0-9]*\)/\1/')
right_pos=$((left_pos + ${area%x*}))
bottom_pos=$((top_pos + ${area#*x}))
for (( r = top_pos; r < bottom_pos; r++)); do
for (( c = left_pos; c < right_pos; c++ )); do
if [[ -z ${fabric[$r,$c]} ]]; then
fabric[$r,$c]=$id
else
fabric[$r,$c]="X"
fi
done
done
# echo "ID: $id; Left: $left_pos; Right: $right_pos; Top: $top_pos; Bottom: $bottom_pos;"
suggestions[$id]="$top_pos.$bottom_pos,$left_pos.$right_pos"
done < "$1"
for id in "${!suggestions[@]}"; do
suggestion=${suggestions[$id]}
vert_position=${suggestion%,*}
horiz_position=${suggestion#*,}
top_pos=${vert_position%.*}
bottom_pos=${vert_position#*.}
left_pos=${horiz_position%.*}
right_pos=${horiz_position#*.}
# echo "ID: $id; Left: $left_pos; Right: $right_pos; Top: $top_pos; Bottom: $bottom_pos;"
for (( r = top_pos; r < bottom_pos; r++)); do
for (( c = left_pos; c < right_pos; c++ )); do
if [[ ${fabric[$r,$c]} == "X" ]]; then
id="X"
fi
done
done
if [[ "$id" != "X" ]]; then
echo "The non-overlaping suggestion is: $id"
exit 0
fi
done