6 filename="${1:-example.txt}"
11 declare -a most_common=( 0 )
12 declare -a all_binaries
15 while read -u 3 line; do
17 if [ $line_length -eq 0 ]; then
20 for (( i=0; i < $line_length; i++ )); do
21 if [ ${#most_common[@]} -lt $i ]; then
24 if [ ${line:$i:1} -eq 1 ]; then
25 ((most_common[$i]+=1))
31 half_line_count=$((line_count/2))
33 inverse_binary_string=""
35 for (( i=0; i < $line_length; i++ )); do
36 if [ ${most_common[$i]} -gt $half_line_count ]; then
38 inverse_binary_string+=0
41 inverse_binary_string+=1
45 total=$((2#$binary_string * 2#$inverse_binary_string))
49 declare -a co2_scrubber
50 declare -a o2_generator
52 # first populate co2_scrubbers and o2_generator
57 if [ ${most_common[0]} -gt $half_line_count ]; then
65 for val in "${all_binaries[@]}"; do
66 if [ ${val:0:1} -eq $co2_filter_digit ]; then
69 if [ ${val:0:1} -eq $o2_generator_digit ]; then
80 for val in "${arr[@]}"; do
82 if [ $part -eq 1 ]; then
89 if [ $ones -gt $zeros ]; then
91 elif [ $ones -lt $zeros ]; then
102 val=$(get_most_common $offset $array_name)
121 if [ ${#arr[@]} -eq 1 ]; then
126 for thing in "${arr[@]}"; do
127 if [ ${thing:$pos:1} -eq $val ]; then
132 arr=("${new_arr[@]}")
135 for ((i=1; i < $line_length; i++)); do
136 co2_filter=$(get_least_common $i co2_scrubber)
137 o2_filter=$(get_most_common $i o2_generator)
139 if [ $co2_filter -eq 2 ]; then
143 if [ $o2_filter -eq 2 ]; then
147 filter_array $co2_filter $i co2_scrubber
148 filter_array $o2_filter $i o2_generator
151 # we've only got 1 in each - so...
152 echo Life Support Rating: $((2#${o2_generator[0]} * 2#${co2_scrubber[0]}))