--- /dev/null
+#!/bin/bash
+
+set -u
+
+read_file() {
+ filename="$1"
+ local IFS=","
+ exec 3<"$filename"
+ read -u 3 -a data
+}
+
+get_answer() {
+ local number=$1
+ local count=$2
+ if [ ${lastseen[$number]+a} ]; then
+ answers[$count]=$((count-${lastseen[$number]}-1))
+ else
+ answers[$count]=0
+ fi
+}
+
+do_nothing() {
+ return
+}
+
+error_echo() {
+ echo "$@" >&2
+}
+
+DEBUG="${DEBUG:-}"
+debug_func=do_nothing
+
+if [ -n "$DEBUG" ]; then
+ debug_func="error_echo"
+fi
+
+filename="${1:-p1_436.txt}"
+
+read_file "$filename"
+
+declare -A lastseen
+declare -a answers
+
+current_answer=-1
+last_answer=-1
+count=0
+
+for number in ${data[@]}; do
+ count=$((count+1))
+ last_answer=$current_answer
+ if [ $last_answer -ge 0 ]; then
+ lastseen[$last_answer]=$((count-1))
+ fi
+ answers[$count]=$number
+ current_answer=$number
+done
+
+while [ $count -lt 2020 ]; do
+ count=$((count+1))
+ get_answer $current_answer $count
+ last_answer=$current_answer
+ lastseen[$last_answer]=$((count-1))
+ current_answer=${answers[$count]}
+ $debug_func "$count: ${answers[$count]}"
+done
+
+echo "Part 1: ${answers[$count]}"
+
+while [ $count -lt 30000000 ]; do
+ count=$((count+1))
+ get_answer $current_answer $count
+ last_answer=$current_answer
+ lastseen[$last_answer]=$((count-1))
+ current_answer=${answers[$count]}
+ $debug_func "$count: ${answers[$count]}"
+done
+
+echo "Part 2: ${answers[$count]}"