6 filename=${1:-example.txt}
 
  11 # each board is a 5*5 grid, so that's handy, we'll store all the boards in one
 
  12 # array, then create a second array that shows which matches we have, we can
 
  13 # select any given board by limiting to a range of 25 values from the array
 
  17 # first line is the draws, so read that in to an array too
 
  30 while read -u 3 -a row; do
 
  33     if [ $line_count -eq 5 ]; then
 
  34         read -u 3 line || true
 
  39 for x in ${boards[@]}; do
 
  45     offset=$((25*$board_number))
 
  49     for val in "${boards[@]:$offset:25}"; do
 
  50         if [ ${checked[$((offset+$count))]} -eq 1 ]; then
 
  56         if [ $((count%5)) -eq 0 ]; then
 
  66     offset=$((25*$board_number))
 
  69     for val in "${boards[@]:$offset:25}"; do
 
  70         if [ $val -eq $drawn_number ]; then
 
  71             checked[$((offset+$count))]=1
 
  78     declare -a row_matches=(0 0 0 0 0)
 
  79     declare -a col_matches=(0 0 0 0 0)
 
  81     for val in "${checked[@]:$offset:25}"; do
 
  82         if [ $val -eq 1 ]; then
 
  83             ((col_matches[$col]+=1))
 
  84             ((row_matches[$row]+=1))
 
  87         if [ $col -eq 5 ]; then
 
  93     for row in "${row_matches[@]}"; do
 
  94         if [ $row -eq 5 ]; then
 
  99     for col in "${col_matches[@]}"; do
 
 100         if [ $col -eq 5 ]; then
 
 108 get_unmarked_count() {
 
 109     local board_number=$1
 
 110     local offset=$((board_number*25))
 
 113     for ((i=0; i<25; i++)); do
 
 114         if [ ${checked[$(($offset+$i))]} -eq 0 ]; then
 
 115             ((count+=${boards[$(($offset+$i))]}))
 
 122 declare -a completed_boards=()
 
 128     for val in "${array[@]}"; do
 
 129         if [ $val -eq $value ]; then
 
 138 first_board_last_number=-1
 
 139 last_board_last_number=-1
 
 141 for number in "${draw[@]}"; do
 
 142     for (( board=0; board<$((${#boards[@]} / 25)); board++ )); do
 
 143         if ( ! in_array $board completed_boards ); then
 
 144             check_board $board $number && { if [ $first_board -eq -1 ]; then first_board=$board; first_board_last_number=$number; fi; completed_boards+=( $board ); } || true
 
 145             if [ ${#completed_boards[@]} -eq $((${#boards[@]} / 25)) ]; then
 
 146                 last_board_last_number=$number
 
 153 echo "Board matched: $first_board"
 
 154 echo "Last number drawn: $first_board_last_number"
 
 156 display_board $first_board
 
 158 unmarked=$(get_unmarked_count $first_board)
 
 160 echo "$unmarked * $first_board_last_number = "$((unmarked*$first_board_last_number))
 
 162 echo "Last board matched: "${completed_boards[-1]}
 
 163 echo "Last number drawn: $last_board_last_number"
 
 165 display_board ${completed_boards[-1]}
 
 167 unmarked=$(get_unmarked_count ${completed_boards[-1]})
 
 169 echo "$unmarked * $last_board_last_number = "$((unmarked*$last_board_last_number))