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