3050832c18aa82f27d8289f5baa34164d2edfd6f
[advent-of-code-2021.git] / day06 / lanternfish.sh
1 #!/bin/bash
2
3 set -u
4 set -e
5
6 declare -a fish
7
8 filename="${1:-example.txt}"
9
10 exec 3<"$filename"
11
12 OLDIFS="$IFS"
13 IFS=","
14 read -u 3 -a fish
15 IFS="$OLDIFS"
16
17 for (( d=1; d<=80; d++ )); do
18     start_fish_count=${#fish[@]}
19     for (( i=0; i<$start_fish_count; i++ )); do
20         if [ ${fish[$i]} -eq 0 ]; then
21             fish+=(8)
22             fish[$i]=6
23         else
24             ((fish[$i]-=1)) || fish[$i]=0
25         fi
26     done
27 done
28
29 echo "After 80 days there are ${#fish[@]} fish"