-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.sh
142 lines (127 loc) · 3.39 KB
/
11.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
test=1 #0
a=()
read -r line
IFS=' ' read -ra a <<< "$line"
echo "nums/ ${a[@]}";echo
if [[ $test == 1 ]]; then
for n in "${a[@]}"; do
echo "item/ $n"
done
fi
trimleading0 () {
local n="$1"
n="${n#"${n%%[!0]*}"}"
n="${n:-0}"
echo "$n"
}
both_parts () {
local a=("${@:1:$#-1}")
local b="${@: -1}"
#if [[ $test == 1 ]]; then
echo "param/a - ${a[@]}"
echo "param/b - $b"
#fi
declare -A Counter
for key in "${a[@]}"; do
Counter["$key"]=$(( ${Counter["$key"]:-0} + 1 ))
done
if [[ $test == 1 ]]; then
for key in "${!Counter[@]}"; do
echo "Counter/ $key: ${Counter[$key]}"
done
fi
B=0
while true; do
if [[ $B == $b ]]; then
break
fi
declare -A tmp
for key in "${!Counter[@]}"; do
#for n in "${a[@]}"; do
val=${Counter[$key]}
#if [[ $n == 0 ]]; then
if [[ $key == 0 ]]; then
tmp["1"]=$(( ${tmp["1"]:-0} + $val ))
#A+=("1")
#elif [[ $(( ${#n} % 2 )) == 0 ]]; then
elif [[ $(( ${#key} % 2 )) == 0 ]]; then
L=$(trimleading0 "${key:0:$(( ${#key} / 2 ))}")
R=$(trimleading0 "${key:$(( ${#key} / 2 ))}")
tmp["$L"]=$(( ${tmp["$L"]:-0} + $val ))
tmp["$R"]=$(( ${tmp["$R"]:-0} + $val ))
#N=${#n}
#L=$(trimleading0 "${n:0:$(( ${#n} / 2 ))}")
#R=$(trimleading0 "${n:$(( ${#n} / 2 ))}")
#A+=("$L")
#A+=("$R")
else
x2024=$(( $key * 2024 ))
tmp["$x2024"]=$(( ${tmp["$x2024"]:-0} + $val ))
#t=($(( $n * 2024 )))
#t=${t#"${t%%[!0]*}"}
#A+=("$t")
fi
done
(( B++ ))
# lets copy
unset Counter
for key in "${!tmp[@]}"; do
Counter["$key"]="${tmp["$key"]}"
done
unset tmp
if [[ $test == 1 ]]; then
echo "count/ $B --- size/ ${#Counter[@]}"
fi
done
echo "final/ ${#Counter[@]}"
pbc=0
ebc=0
res=0
for key in "${!Counter[@]}"; do
pbc=$(printf "%s + %s\n" "$pbc" "${Counter["$key"]}" | bc)
ebc=$( echo "$ebc + ${Counter["$key"]}" | bc )
(( res+=Counter["$key"] ))
### diff/ ways to compute the same thing
done
echo "printf/bc $pbc"
echo "echo/bc $ebc"
echo "res/ $res"
}
#both_parts "${a[@]}" "25"
both_parts "${a[@]}" "75"
### deprecated
deprecated_p1 () {
local a=("${@}")
local b=0
while true; do
if [[ $b == 25 ]]; then
break
fi
local A=()
for n in "${a[@]}"; do
if [[ $n == 0 ]]; then
A+=("1")
elif [[ $(( ${#n} % 2 )) == 0 ]]; then
N=${#n}
L=$(trimleading0 "${n:0:$(( ${#n} / 2 ))}")
R=$(trimleading0 "${n:$(( ${#n} / 2 ))}")
A+=("$L")
A+=("$R")
else
t=($(( $n * 2024 )))
t=${t#"${t%%[!0]*}"}
A+=("$t")
fi
done
(( b++ ))
# copy a list
unset a
a=("${A[@]}")
unset A
echo "count/ $b --- size/ ${#a[@]}"
done
echo "final/ ${#a[@]}"
#echo "${a[@]}"
}
# deprecated_p1 "${a[@]}"