-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.sh
executable file
·79 lines (74 loc) · 1.58 KB
/
part1.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
#!/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;
visible=0;
# iterate over array and compare sizes of maimum sizes
for (( row=0; row < rows; row++ )); do
for (( col=0; col < cols; col++ )); do
currentSize=${map["$row,$col"]};
# get upper max
max=-1;
for (( i=0; i < row; i++ )); do
size=${map["$i,$col"]};
if [[ $max -lt $size ]]; then
max=$size;
fi
done
if [[ $max -lt $currentSize ]]; then
((visible++));
continue;
fi
# get right max
max=-1;
for (( i=0; i < col; i++ )); do
size=${map["$row,$i"]};
if [[ $max -lt $size ]]; then
max=$size;
fi
done
if [[ $max -lt $currentSize ]]; then
((visible++));
continue;
fi
# get lower max
max=-1;
for (( i=row+1; i < rows; i++ )); do
size=${map["$i,$col"]};
if [[ $max -lt $size ]]; then
max=$size;
fi
done
if [[ $max -lt $currentSize ]]; then
((visible++));
continue;
fi
# get left max
max=-1;
for (( i=col+1; i < cols; i++ )); do
size=${map["$row,$i"]};
if [[ $max -lt $size ]]; then
max=$size;
fi
done
if [[ $max -lt $currentSize ]]; then
((visible++));
fi
done
done
echo "Answer: $visible";