168 lines
5.5 KiB
Bash
Executable File
168 lines
5.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
representations=()
|
|
max_magnitude=0
|
|
while read line; do
|
|
representation=()
|
|
nests=0
|
|
for ((i=0;i<${#line};i++)); do
|
|
case ${line:$i:1} in
|
|
'[')
|
|
nests=$((nests+1))
|
|
;;
|
|
']')
|
|
nests=$((nests-1))
|
|
;;
|
|
',')
|
|
;;
|
|
*)
|
|
representation+=( $nests':'${line:$i:1} )
|
|
;;
|
|
esac
|
|
done
|
|
representations+=( $(sed 's/ /_/g' <<< ${representation[@]}) )
|
|
done
|
|
|
|
for ((k=0;k<${#representations[@]};k++)); do
|
|
for ((j=0;j<${#representations[@]};j++)); do
|
|
if [ $j -eq $k ]; then
|
|
continue
|
|
fi
|
|
representation=( $(sed 's/_/ /g' <<< ${representations[$k]}) )
|
|
sum_representation=( $(sed 's/_/ /g' <<< ${representations[$j]}) )
|
|
for ((i=0;i<${#representation[@]};i++)); do
|
|
node=${representation[$i]}
|
|
node_nests=${node%':'*}
|
|
node_value=${node#*':'}
|
|
representation[$i]=$((node_nests+1))':'$node_value
|
|
done
|
|
for ((i=0;i<${#sum_representation[@]};i++)); do
|
|
node=${sum_representation[$i]}
|
|
node_nests=${node%':'*}
|
|
node_value=${node#*':'}
|
|
representation+=( $((node_nests+1))':'$node_value )
|
|
done
|
|
changes=1
|
|
while [ $changes -eq 1 ]; do
|
|
changes=0
|
|
for ((i=0;i<${#representation[@]};i++)); do
|
|
node=${representation[$i]}
|
|
node_nests=${node%':'*}
|
|
node_value=${node#*':'}
|
|
if [ $node_nests -gt 4 ]; then
|
|
following_node=${representation[$((i+1))]}
|
|
following_node_nests=${following_node%':'*}
|
|
following_node_value=${following_node#*':'}
|
|
if [ $node_nests -eq $following_node_nests ]; then
|
|
changes=1
|
|
new_representation=()
|
|
if [ $i -gt 0 ]; then
|
|
prev_value=${representation[$((i-1))]}
|
|
prev_nests=${prev_value%':'*}
|
|
prev_value=${prev_value#*':'}
|
|
representation[$((i-1))]=$prev_nests':'$((prev_value+node_value))
|
|
new_representation+=( ${representation[@]:0:$i} )
|
|
fi
|
|
new_representation+=( $((node_nests-1))':'0 )
|
|
if [ $i -lt $((${#representation[@]}-2)) ]; then
|
|
next_value=${representation[$((i+2))]}
|
|
next_nests=${next_value%':'*}
|
|
next_value=${next_value#*':'}
|
|
representation[$((i+2))]=$next_nests':'$((next_value+following_node_value))
|
|
new_representation+=( ${representation[@]:$((i+2))} )
|
|
fi
|
|
representation=( ${new_representation[@]} )
|
|
break
|
|
fi
|
|
fi
|
|
done
|
|
if [ $changes -eq 0 ]; then
|
|
for ((i=0;i<${#representation[@]};i++)); do
|
|
node=${representation[$i]}
|
|
node_nests=${node%':'*}
|
|
node_value=${node#*':'}
|
|
if [ $node_value -gt 9 ]; then
|
|
changes=1
|
|
new_representation=()
|
|
if [ $i -gt 0 ]; then
|
|
new_representation+=( ${representation[@]:0:$i} )
|
|
fi
|
|
value_left=$(($node_value/2))
|
|
value_right=$value_left
|
|
if [ $((node_value%2)) -eq 1 ]; then
|
|
value_right=$((value_right+1))
|
|
fi
|
|
new_representation+=( $((node_nests+1))':'$value_left $((node_nests+1))':'$value_right )
|
|
if [ $i -lt $((${#representation[@]}-1)) ]; then
|
|
new_representation+=( ${representation[@]:$((i+1))} )
|
|
fi
|
|
representation=( ${new_representation[@]} )
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
done
|
|
|
|
declare -A tree_representation
|
|
|
|
last_key=''
|
|
for node in ${representation[@]}; do
|
|
node_nests=${node%':'*}
|
|
node_value=${node#*':'}
|
|
current_key=''
|
|
if [ -z $last_key ]; then
|
|
current_key=$(for ((i=0;i<$node_nests;i++)); do echo -n 0; done)
|
|
tree_representation[$current_key]=$node_value
|
|
else
|
|
if [ ${#last_key} -eq $node_nests ]; then
|
|
current_key=$(( 2#$last_key + 2#01 ))
|
|
current_key=$(echo "ibase=A;obase=2;$current_key" | bc)
|
|
while [ ${#current_key} -lt $node_nests ]; do
|
|
current_key=0$current_key
|
|
done
|
|
tree_representation[$current_key]=$node_value
|
|
elif [ ${#last_key} -lt $node_nests ]; then
|
|
current_key=$(( 2#$last_key + 2#01 ))
|
|
current_key=$(echo "ibase=A;obase=2;$current_key" | bc)
|
|
while [ ${#current_key} -lt ${#last_key} ]; do
|
|
current_key=0$current_key
|
|
done
|
|
while [ ${#current_key} -lt $node_nests ]; do
|
|
current_key=${current_key}0
|
|
done
|
|
tree_representation[$current_key]=$node_value
|
|
else
|
|
current_key=${last_key:0:$node_nests}
|
|
current_key=$(( 2#$current_key + 2#01 ))
|
|
current_key=$(echo "ibase=A;obase=2;$current_key" | bc)
|
|
while [ ${#current_key} -lt $node_nests ]; do
|
|
current_key=0$current_key
|
|
done
|
|
tree_representation[$current_key]=$node_value
|
|
fi
|
|
fi
|
|
last_key=$current_key
|
|
done
|
|
|
|
magnitude=0
|
|
for tree_representation_node in ${!tree_representation[@]}; do
|
|
temp_magnitude=${tree_representation[$tree_representation_node]}
|
|
for ((i=0;i<${#tree_representation_node};i++)); do
|
|
if [ ${tree_representation_node:$i:1} -eq 0 ]; then
|
|
temp_magnitude=$((temp_magnitude*3))
|
|
else
|
|
temp_magnitude=$((temp_magnitude*2))
|
|
fi
|
|
done
|
|
magnitude=$((magnitude+temp_magnitude))
|
|
done
|
|
if [ $magnitude -gt $max_magnitude ]; then
|
|
max_magnitude=$magnitude
|
|
fi
|
|
unset tree_representation
|
|
done
|
|
done
|
|
|
|
|
|
echo $max_magnitude
|