Compare commits

...

2 Commits

Author SHA1 Message Date
415f757054 Fixed missing paste. Working part 1 2024-12-15 16:18:50 +08:00
615f9ec252 Added more wall tests. Made more verbose GPS counting. 2024-12-15 16:14:11 +08:00

View File

@ -6,6 +6,7 @@ MAP_WIDTH=$(( $( head -1 "$MAP_FILE" | wc -c ) -1 ))
#MAP_HEIGHT=$( < "$MAP_FILE" wc -l )
# Load map
read -r -a TEST_ARRAY <<< "$( < "$MAP_FILE" paste -s -d "" | sed -E 's/(.)(.)/\1 \2 /g' )"
read -r -a MAP_ARRAY <<< "$( < "$MAP_FILE" paste -s -d "" | sed -E 's/(.)(.)/\1 \2 /g' )"
printf "%s " "${MAP_ARRAY[@]}" | fold -w $(( MAP_WIDTH * 2 ))
printf "\n"
@ -40,6 +41,7 @@ LEFT=-1
RIGHT=1
# Iterate through directions
MOVES=0
while read -r DIRECTION
do
@ -69,9 +71,11 @@ do
done
if [[ ${MAP_ARRAY[$END_POSITION]} == '#' ]]
then
printf "Next: %s End: %s Stacked boxes. No change.\n" "${MAP_ARRAY[$NEXT_POSITION]}" "${MAP_ARRAY[$END_POSITION]}"
: # Boxes are stacked up against wall
elif [[ ${MAP_ARRAY[$END_POSITION]} == "." ]]
then
printf "Next: %s End: %s Robot: %s Move boxes and robot\n" "${MAP_ARRAY[$NEXT_POSITION]}" "${MAP_ARRAY[$END_POSITION]}" "${MAP_ARRAY[$ROBOT_POSITION]}"
# Move the box
MAP_ARRAY[END_POSITION]=O
MAP_ARRAY[NEXT_POSITION]=.
@ -82,16 +86,20 @@ do
fi
elif [[ ${MAP_ARRAY[$NEXT_POSITION]} == "#" ]] # Wall in front, do nothing
then
continue
printf "Next: %s Skipping\n" "${MAP_ARRAY[$NEXT_POSITION]}"
:
elif [[ ${MAP_ARRAY[$NEXT_POSITION]} == "." ]] # Empty space, move forward
then
printf "Next: %s Move robot forward\n" "${MAP_ARRAY[$NEXT_POSITION]}"
# Move the robot
MAP_ARRAY[ROBOT_POSITION]=.
ROBOT_POSITION=$NEXT_POSITION
MAP_ARRAY[ROBOT_POSITION]=@
fi
(( MOVES++ ))
done <<< "$( < "$DIRECTIONS_FILE" sed -E 's/(.)(.)/\1 \2 /g' | tr ' ' '\n' )"
done <<< "$( < "$DIRECTIONS_FILE" paste -s -d "" | sed -E 's/(.)(.)/\1 \2 /g' | tr ' ' '\n' )"
printf "Moves: %s\n" "$MOVES"
printf "%s " "${MAP_ARRAY[@]}" | fold -w $(( MAP_WIDTH * 2 ))
printf "\n"
@ -104,9 +112,24 @@ do
if [[ ${MAP_ARRAY[i]} == "O" ]]
then
(( BOX_COUNT++ ))
(( SUM+=( (i / MAP_WIDTH) * 100 ) )) # Add Y axis
(( SUM+=( i % MAP_WIDTH ) )) # Add x axis
X=$(( i % MAP_WIDTH ))
Y=$(( i / MAP_WIDTH * 100 ))
GPS=$(( X + Y ))
printf "Box %s X: %s Y: %s GPS: %s\n" "$i" "$X" "$Y" "$GPS"
(( SUM+=GPS ))
fi
done
printf "Sum: %s\n" "$SUM"
printf "Box count: %s\n" "$BOX_COUNT"
# Compare and test if walls have moved
for (( i=0; i<MAP_LEN; i++ ))
do
if [[ ${TEST_ARRAY[i]} == "#" ]]
then
if [[ ${TEST_ARRAY[i]} != "${MAP_ARRAY[i]}" ]]
then
printf "Index %s Wall changed.\n" "$i"
fi
fi
done