Broken part 2
This commit is contained in:
78
2024/day-14/solution-2.sh
Normal file
78
2024/day-14/solution-2.sh
Normal file
@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env/bash
|
||||
|
||||
FILE=input
|
||||
MAP_WIDTH=101
|
||||
MAP_HEIGHT=103
|
||||
MEDIAN_WIDTH=$(( MAP_WIDTH / 2 - 1 ))
|
||||
if [[ $(( MAP_WIDTH % 2 )) -eq 1 ]]
|
||||
then
|
||||
(( MEDIAN_WIDTH++ ))
|
||||
fi
|
||||
MEDIAN_HEIGHT=$(( MAP_HEIGHT / 2 - 1 ))
|
||||
if [[ $(( MAP_HEIGHT % 2 )) -eq 1 ]]
|
||||
then
|
||||
(( MEDIAN_HEIGHT++ ))
|
||||
fi
|
||||
printf "Median width: %s, median height: %s\n" "$MEDIAN_WIDTH" "$MEDIAN_HEIGHT"
|
||||
|
||||
# Load bot positions and velocities
|
||||
read -r -a BOT_ARRAY <<< "$( grep -Eo '[-0-9]*' "$FILE" | paste -s -d " " )"
|
||||
#printf "%s " "${BOT_ARRAY[@]}"
|
||||
#printf "\n"
|
||||
printf "Bot array size: %s\n" "${#BOT_ARRAY[@]}"
|
||||
|
||||
# Load arrangements for every second
|
||||
for SECONDS in $( seq 1 100 )
|
||||
do
|
||||
IFS=';'; read -r -a FINAL_ARRAY <<< "$(
|
||||
for (( i=0; i<${#BOT_ARRAY[@]}; i+=4 ))
|
||||
do
|
||||
PX=${BOT_ARRAY[i]}
|
||||
PY=${BOT_ARRAY[i+1]}
|
||||
VX=${BOT_ARRAY[i+2]}
|
||||
VY=${BOT_ARRAY[i+3]}
|
||||
FINAL_X=$(( PX + ( VX * SECONDS ) ))
|
||||
FINAL_Y=$(( PY + ( VY * SECONDS ) ))
|
||||
printf "%s %s;" "$FINAL_X" "$FINAL_Y"
|
||||
done <<< "${BOT_ARRAY[@]}"
|
||||
)"
|
||||
unset IFS
|
||||
|
||||
# Generate "empty" map
|
||||
MAP_LEN=$(( MAP_HEIGHT * MAP_WIDTH ))
|
||||
read -r -a MAP_ARRAY <<< "$(
|
||||
for (( i=0; i<MAP_LEN; i++ ))
|
||||
do
|
||||
printf ". "
|
||||
done
|
||||
)"
|
||||
|
||||
# Calculate and map bot positions on the map
|
||||
while read -r RAW_X RAW_Y
|
||||
do
|
||||
|
||||
# Adjust positions to be on map
|
||||
PX=$(( RAW_X % MAP_WIDTH ))
|
||||
if [[ $PX -lt 0 ]]
|
||||
then
|
||||
PX=$(( MAP_WIDTH + PX ))
|
||||
fi
|
||||
PY=$(( RAW_Y % MAP_HEIGHT ))
|
||||
if [[ $PY -lt 0 ]]
|
||||
then
|
||||
PY=$(( MAP_HEIGHT + PY ))
|
||||
fi
|
||||
|
||||
# Calculate serial position
|
||||
POSITION=$(( PX + (PY*MAP_WIDTH -1) ))
|
||||
MAP_ARRAY[POSITION]=X
|
||||
|
||||
done <<< "$( printf "%s\n" "${FINAL_ARRAY[@]}" )"
|
||||
|
||||
# Print out map for manual review
|
||||
printf "%s" "${MAP_ARRAY[@]}" | fold -w $(( MAP_WIDTH ))
|
||||
printf "\n"
|
||||
printf "\n"
|
||||
|
||||
unset FINAL_ARRAY
|
||||
done
|
Reference in New Issue
Block a user