-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.sh
78 lines (67 loc) · 1.6 KB
/
05.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
#!bin/bash
bloc1=""
bloc2=""
crossed=false
while read -r line; do
if [[ -z "$line" ]]; then
crossed=true
else
if $crossed; then
bloc2+="$line"$'\n'
else
bloc1+="$line"$'\n'
fi
fi
done
bloc1="${bloc1%$'\n'}"
bloc2="${bloc2%$'\n'}"
#echo "$bloc1 /---dbg---/ $bloc2"
#DONE
# check if val in set[key]
# sort w/ a set of rules
# STEP/ mimic `defaultdict(set)`, parse the rules
declare -a isafter
insert() {
local a=$1
local b=$2
if [[ ! " ${isafter[$a]} " =~ " $b " ]]; then
isafter["$a"]+="$b "
fi
}
while IFS='|' read -r a sorted; do
insert "$a" "$sorted"
done <<< "$bloc1"
#for key in "${!isafter[@]}"; do echo "k: $key, set: ${isafter[$key]}";done
# STEP/ sorting
# bubble sort using isbefore|isafter set from bloc1
r1=0
r2=0
while read -r line; do
IFS=',' read -ra original <<< "$line"
N=${#original[@]}
sorted=("${original[@]}")
for (( i = 0; i < N; i++ )); do
for (( j = 0; j < N - i - 1; j++ )); do
if [[ " ${isafter[${sorted[j + 1]}]} " =~ " ${sorted[j]} " ]]; then
#echo "swap/"
n="${sorted[j]}"
sorted[j]="${sorted[j + 1]}"
sorted[j + 1]="$n"
fi
done
done
ok=true
for (( i = 0; i < N; i++ )); do
if [[ "${original[$i]}" != "${sorted[$i]}" ]]; then
ok=false
break
fi
done
if [[ $ok == true ]]; then
((r1 += ${original[N / 2]}))
else
((r2 += ${sorted[N / 2]}))
fi
done <<< "$bloc2"
echo "part 1: $r1"
echo "part 2: $r2"