File tree 1 file changed +25
-32
lines changed
1 file changed +25
-32
lines changed Original file line number Diff line number Diff line change @@ -5,60 +5,53 @@ FILE="$1"
5
5
declare -A deps
6
6
7
7
parse_rule () {
8
- first=" ${1%% |* } "
9
- second=" ${1##* |} "
10
- if [[ -n " ${deps[$second]} " ]]; then
11
- first=" ${deps[$second]} $first "
8
+ IFS=' |' read a b <<< " $line"
9
+ if [[ -n " ${deps[$b]} " ]]; then
10
+ a=" ${deps[$b]} $a "
12
11
fi
13
- deps[$second ]=" $first "
12
+ deps[$b ]=" $a "
14
13
}
15
14
16
15
split () {
17
- # Usage: split "string" "delimiter"
18
16
IFS=$' \n ' read -d " " -ra arr <<< " ${1//$2/$'\n'}"
19
17
}
20
18
21
19
parse_update () {
22
- local -a arr
23
- local INVALID
24
- local -a violations
25
- split " $1 " ,
20
+ local -a arr violations
21
+ local invalid
22
+ split " $line " ,
26
23
local -a pages=(" ${arr[@]} " )
27
24
for page in " ${pages[@]} " ; do
28
25
split " ${deps[$page]} " ' '
29
26
for v in " ${violations[@]} " ; do
30
27
if [[ " $v " == " $page " ]]; then
31
- INVALID =yes
28
+ invalid =yes
32
29
fi
33
30
done
34
31
violations+=(" ${arr[@]} " )
35
32
done
36
- [[ -z " $INVALID " ]] && echo " ${pages[@]} "
33
+ [[ -z " $invalid " ]] && echo " ${pages[@]} "
37
34
}
38
35
39
36
middle () {
40
- local line
41
- local -a arr
42
- while IFS= read -r line; do
43
- split " $line " ' '
44
- LEN=" ${# arr[@]} "
45
- (( LEN/= 2 ))
46
- echo ${arr["$LEN"]}
37
+ awk ' {print $((NF+1)/2)}'
38
+ }
39
+
40
+ main () {
41
+ while read line; do
42
+ test -z " $line " && break
43
+ parse_rule
44
+ done
45
+
46
+ while read line; do
47
+ parse_update
47
48
done
48
49
}
49
50
50
- while IFS= read -r line; do
51
- if [[ " $line " == " " ]]; then
52
- section=2
53
- continue
54
- fi
51
+ sum () {
52
+ paste -sd+ | bc
53
+ }
55
54
56
- if [[ $section -eq 2 ]]; then
57
- parse_update " $line "
58
- else
59
- parse_rule " $line "
60
- fi
61
- done < " $FILE " \
55
+ main < " $FILE " \
62
56
| middle \
63
- | paste -sd+ \
64
- | bc
57
+ | sum
You can’t perform that action at this time.
0 commit comments