Start to rewrite of 5:2
This commit is contained in:
parent
93c9afd3d2
commit
665b592b3b
136
5/2/solution.sh
136
5/2/solution.sh
@ -1,5 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# INCOMPLETE
|
||||||
|
|
||||||
set -o errexit
|
set -o errexit
|
||||||
set -o nounset
|
set -o nounset
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
@ -7,89 +9,89 @@ if [[ "${TRACE-0}" == "1" ]]; then
|
|||||||
set -o xtrace
|
set -o xtrace
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cd "$(dirname "$0")"
|
|
||||||
|
|
||||||
INPUT_FILE="$1"
|
INPUT_FILE="$1"
|
||||||
|
|
||||||
get_seed_pairs ()
|
gen_maps ()
|
||||||
{
|
{
|
||||||
IFS=" " read -r -a seeds <<< "$(grep seeds "${INPUT_FILE}" | cut -d ' ' -f 2-)"
|
local map_count paragraph_num lines name
|
||||||
seed_count="${#seeds[@]}"
|
map_count=$(awk -v RS= 'END {print NR}' "${INPUT_FILE}")
|
||||||
pairs=$((seed_count/2))
|
map_count=$((map_count-1)) # one paragraph in file is seeds
|
||||||
for ((i=0; i<pairs; i++)); do
|
for ((i=1; i<=map_count; i++)); do
|
||||||
j=$((i*2))
|
paragraph_num=$((i+1)) # number of paragraph in input file
|
||||||
k=$((i*2+1))
|
lines="$(awk -v RS= "{if(NR == ${paragraph_num}) print}" "${INPUT_FILE}"| tail -n +2)"
|
||||||
seed_pairs[${seeds[${j}]}]="${seeds[${k}]}"
|
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
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
gen_map ()
|
gen_seed_pairs ()
|
||||||
{
|
{
|
||||||
# Generates files for given index of map in input file.
|
local seed_count pair_count i_start i_length start length end
|
||||||
local paragraph map_name map_content
|
IFS=" " read -r -a seeds <<< "$(grep seeds "${INPUT_FILE}" | cut -d ' ' -f 2-)"
|
||||||
paragraph=$(($1+1)) # number of paragraph in input file
|
seed_count="${#seeds[@]}"
|
||||||
map_name=$(awk -v RS= "{if(NR == ${paragraph}) print}" "${INPUT_FILE}" | head -n 1 | cut -d ' ' -f 1)
|
pair_count=$((seed_count/2))
|
||||||
map_content=$(awk -v RS= "{if(NR == ${paragraph}) print}" "${INPUT_FILE}"| tail -n +2)
|
for ((i=0; i<pair_count; i++)); do
|
||||||
echo "${map_content}" > "${map_name}.map"
|
i_start=$((i*2))
|
||||||
maps+=("${map_name}.map")
|
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
|
local dest_start source_start length dest_end source_end
|
||||||
# Prints the mapping of the seed number in the give map.
|
for n in "${maps[@]}"; do
|
||||||
local map seed dest_start source_start length diff
|
declare -a new_pairs=()
|
||||||
map="$1"
|
declare -n map="${n}"
|
||||||
seed="$2"
|
for line in "${map[@]}"; do
|
||||||
while read -r line; do
|
dest_start="$(cut -d ' ' -f 1 <<< "${line}")"
|
||||||
dest_start=$(cut -d ' ' -f 1 <<< "${line}")
|
source_start="$(cut -d ' ' -f 2 <<< "${line}")"
|
||||||
source_start=$(cut -d ' ' -f 2 <<< "${line}")
|
length="$(cut -d ' ' -f 3 <<< "${line}")"
|
||||||
length=$(cut -d ' ' -f 3 <<< "${line}")
|
dest_end=$((dest_start+length-1))
|
||||||
if [[ ${seed} -ge ${source_start} && ${seed} -le $((source_start+length-1)) ]]; then
|
source_end=$((source_start+length-1))
|
||||||
diff=$((seed-source_start))
|
if [[ ${#new_pairs[@]} -eq 0 ]]; then
|
||||||
echo -n $((dest_start+diff))
|
declare -n source_pairs="seed_pairs"
|
||||||
return
|
else
|
||||||
fi
|
declare -n source_pairs="new_pairs"
|
||||||
done < "${map}"
|
fi
|
||||||
echo -n "${seed}" # no mapping
|
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 ()
|
main ()
|
||||||
{
|
{
|
||||||
declare -A seed_pairs=()
|
|
||||||
declare -a maps=()
|
declare -a maps=()
|
||||||
local map_count destination lowest length seed
|
declare -a seed_pairs=() # start,end
|
||||||
get_seed_pairs
|
gen_maps
|
||||||
map_count=$(awk -v RS= 'END {print NR}' "${INPUT_FILE}")
|
gen_seed_pairs
|
||||||
map_count=$((map_count-1)) # one of the paragraphs in the file is seeds
|
get_lowest
|
||||||
for ((i=1; i<=map_count; i++)); do
|
|
||||||
gen_map "${i}"
|
echo "${#seed_pairs[@]}"
|
||||||
|
for i in "${seed_pairs[@]}"; do
|
||||||
|
echo "$i"
|
||||||
done
|
done
|
||||||
lowest=''
|
|
||||||
for s in "${!seed_pairs[@]}"; do
|
# for map in "${maps[@]}"; do
|
||||||
length=${seed_pairs[${s}]}
|
# declare -n array="${map}"
|
||||||
echo "running from start ${s} length ${length}"
|
# echo "printing ${map} ${#array[@]}"
|
||||||
for ((z=0; z<length; z++)); do
|
# for ((i=0; i<${#array[@]}; i++)); do
|
||||||
y=$((s+z))
|
# echo "${array[${i}]}"
|
||||||
destination=''
|
# done
|
||||||
for map in "${maps[@]}"; do
|
# done
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main
|
main
|
||||||
|
Loading…
Reference in New Issue
Block a user