-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.sh
executable file
·61 lines (56 loc) · 1.43 KB
/
part2.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
#!/bin/bash
file="./input.txt";
# read the file and put all numbers in a 1 1/2 dimensional array
declare -A map;
rows=0;
cols=0;
while read -r line; do
if [ -z "$line" ]; then
echo "unexpected empty line";
exit 1;
fi
cols=0;
while [ $cols -lt ${#line} ]; do
vec="$rows,$cols";
map[$vec]=${line:$cols:1};
((cols++));
done
((rows++));
done < $file;
sceneScore=0;
# iterate over array and compare sizes of maimum sizes
for (( row=1; row < rows - 1; row++ )); do
for (( col=1; col < cols - 1; col++ )); do
currentSize=${map["$row,$col"]};
for (( i = row - 1, v = 1; i >= 0; i--, v++ )); do
upScore=$v;
if [ ${map["$i,$col"]} -ge $currentSize ]; then
break;
fi
done
for (( i = col + 1, v = 1; i < cols; i++, v++ )); do
rightScore=$v;
if [ ${map["$row,$i"]} -ge $currentSize ]; then
break;
fi
done
for (( i = row + 1, v = 1; i < rows; i++, v++ )); do
downScore=$v;
if [ ${map["$i,$col"]} -ge $currentSize ]; then
break;
fi
done
for (( i = col - 1, v = 1; i >= 0; i--, v++ )); do
leftScore=$v;
if [ ${map["$row,$i"]} -ge $currentSize ]; then
break;
fi
done
tmp=$((upScore * rightScore * downScore * leftScore));
if [ $tmp -gt $sceneScore ]; then
sceneScore=$tmp;
echo "[$row,$col] $upScore $rightScore $downScore $leftScore"
fi
done
done
echo "Answer: $sceneScore";