Day 6
[advent-of-code-2021.git] / day06 / lanternfish.sh
diff --git a/day06/lanternfish.sh b/day06/lanternfish.sh
new file mode 100755 (executable)
index 0000000..3050832
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+set -u
+set -e
+
+declare -a fish
+
+filename="${1:-example.txt}"
+
+exec 3<"$filename"
+
+OLDIFS="$IFS"
+IFS=","
+read -u 3 -a fish
+IFS="$OLDIFS"
+
+for (( d=1; d<=80; d++ )); do
+    start_fish_count=${#fish[@]}
+    for (( i=0; i<$start_fish_count; i++ )); do
+        if [ ${fish[$i]} -eq 0 ]; then
+            fish+=(8)
+            fish[$i]=6
+        else
+            ((fish[$i]-=1)) || fish[$i]=0
+        fi
+    done
+done
+
+echo "After 80 days there are ${#fish[@]} fish"