#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace fi cd "$(dirname "$0")" INPUT_FILE="$1" get_seed_pairs () { IFS=" " read -r -a seeds <<< "$(grep seeds "${INPUT_FILE}" | cut -d ' ' -f 2-)" seed_count="${#seeds[@]}" pairs=$((seed_count/2)) for ((i=0; i "${map_name}.map" maps+=("${map_name}.map") } map_seed () { # Usage: map_seed MAP_FILE SEED_NUMBER # Prints the mapping of the seed number in the give map. local map seed dest_start source_start length diff map="$1" seed="$2" while read -r line; do dest_start=$(cut -d ' ' -f 1 <<< "${line}") source_start=$(cut -d ' ' -f 2 <<< "${line}") length=$(cut -d ' ' -f 3 <<< "${line}") if [[ ${seed} -ge ${source_start} && ${seed} -le $((source_start+length-1)) ]]; then diff=$((seed-source_start)) echo -n $((dest_start+diff)) return fi done < "${map}" echo -n "${seed}" # no mapping } main () { declare -A seed_pairs=() declare -a maps=() local map_count destination lowest length seed get_seed_pairs map_count=$(awk -v RS= 'END {print NR}' "${INPUT_FILE}") map_count=$((map_count-1)) # one of the paragraphs in the file is seeds for ((i=1; i<=map_count; i++)); do gen_map "${i}" done lowest='' for s in "${!seed_pairs[@]}"; do length=${seed_pairs[${s}]} echo "running from start ${s} length ${length}" for ((z=0; z