Start to rewrite of 5:2

This commit is contained in:
Abdulkadir Furkan Şanlı 2023-12-06 18:06:47 +01:00
parent 93c9afd3d2
commit 665b592b3b
Signed by: afk
GPG Key ID: C8F00588EE6ED1FE

View File

@ -1,5 +1,7 @@
#!/usr/bin/env bash
# INCOMPLETE
set -o errexit
set -o nounset
set -o pipefail
@ -7,89 +9,89 @@ if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
cd "$(dirname "$0")"
INPUT_FILE="$1"
get_seed_pairs ()
gen_maps ()
{
IFS=" " read -r -a seeds <<< "$(grep seeds "${INPUT_FILE}" | cut -d ' ' -f 2-)"
seed_count="${#seeds[@]}"
pairs=$((seed_count/2))
for ((i=0; i<pairs; i++)); do
j=$((i*2))
k=$((i*2+1))
seed_pairs[${seeds[${j}]}]="${seeds[${k}]}"
local map_count paragraph_num lines name
map_count=$(awk -v RS= 'END {print NR}' "${INPUT_FILE}")
map_count=$((map_count-1)) # one paragraph in file is seeds
for ((i=1; i<=map_count; i++)); do
paragraph_num=$((i+1)) # number of paragraph in input file
lines="$(awk -v RS= "{if(NR == ${paragraph_num}) print}" "${INPUT_FILE}"| tail -n +2)"
name="$(awk -v RS= "{if(NR == ${paragraph_num}) print}" "${INPUT_FILE}" | head -n 1 | cut -d ' ' -f 1 | tr - _)"
declare -n array="${name}"
while read -r line; do
array+=("${line}")
done <<< "${lines}"
maps+=("${name}")
done
}
gen_map ()
gen_seed_pairs ()
{
# Generates files for given index of map in input file.
local paragraph map_name map_content
paragraph=$(($1+1)) # number of paragraph in input file
map_name=$(awk -v RS= "{if(NR == ${paragraph}) print}" "${INPUT_FILE}" | head -n 1 | cut -d ' ' -f 1)
map_content=$(awk -v RS= "{if(NR == ${paragraph}) print}" "${INPUT_FILE}"| tail -n +2)
echo "${map_content}" > "${map_name}.map"
maps+=("${map_name}.map")
local seed_count pair_count i_start i_length start length end
IFS=" " read -r -a seeds <<< "$(grep seeds "${INPUT_FILE}" | cut -d ' ' -f 2-)"
seed_count="${#seeds[@]}"
pair_count=$((seed_count/2))
for ((i=0; i<pair_count; i++)); do
i_start=$((i*2))
i_length=$((i*2+1))
start=${seeds[${i_start}]}
length=${seeds[${i_length}]}
end=$((start+length-1))
seed_pairs+=("${start},${end}")
done
}
map_seed ()
get_lowest ()
{
# 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
local dest_start source_start length dest_end source_end
for n in "${maps[@]}"; do
declare -a new_pairs=()
declare -n map="${n}"
for line in "${map[@]}"; do
dest_start="$(cut -d ' ' -f 1 <<< "${line}")"
source_start="$(cut -d ' ' -f 2 <<< "${line}")"
length="$(cut -d ' ' -f 3 <<< "${line}")"
dest_end=$((dest_start+length-1))
source_end=$((source_start+length-1))
if [[ ${#new_pairs[@]} -eq 0 ]]; then
declare -n source_pairs="seed_pairs"
else
declare -n source_pairs="new_pairs"
fi
for ((i=0; i<${#source_pairs[@]}; i++)); do
interim_pairs+=(source)
seed_start="$(cut -d ',' -f 1 <<< "${source_pairs[${i}]}")"
seed_end="$(cut -d ',' -f 2 <<< "${source_pairs[${i}]}")"
done
done
done
declare -a new_pairs
seed_pairs=("${new_pairs[@]}")
}
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}"
declare -a seed_pairs=() # start,end
gen_maps
gen_seed_pairs
get_lowest
echo "${#seed_pairs[@]}"
for i in "${seed_pairs[@]}"; do
echo "$i"
done
lowest=''
for s in "${!seed_pairs[@]}"; do
length=${seed_pairs[${s}]}
echo "running from start ${s} length ${length}"
for ((z=0; z<length; z++)); do
y=$((s+z))
destination=''
for map in "${maps[@]}"; do
if [[ -z ${destination} ]]; then
destination=$(map_seed "${map}" "${y}")
else
destination=$(map_seed "${map}" "${destination}")
fi
done
if [[ -z ${lowest} ]]; then
lowest="${destination}"
elif [[ ${destination} -lt ${lowest} ]]; then
lowest="${destination}"
fi
echo "iteration ${z} seed ${y} destination ${destination}"
done
echo "interim lowest: ${lowest}"
done
echo "${lowest}"
rm -f ./*.map
# for map in "${maps[@]}"; do
# declare -n array="${map}"
# echo "printing ${map} ${#array[@]}"
# for ((i=0; i<${#array[@]}; i++)); do
# echo "${array[${i}]}"
# done
# done
}
main