Files
advent-of-code-2021/13/02.sh
2021-12-13 13:59:15 +01:00

63 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
declare -A dots
folds=()
min_x=-1
min_y=-1
while read line; do
if [[ $line == *','* ]]; then
dots[$line]=1
else
folds+=( $(sed 's/fold along //' <<< $line) )
fi
done
for fold in ${folds[@]}; do
fold_axis=$(sed 's/=.*$//' <<< $fold)
fold_coord=$(sed 's/^.*=//' <<< $fold)
new_dots=()
todel_dots=()
if [[ $fold_axis == 'x' ]] && { [ $fold_coord -lt $min_x ] || [ $min_x -eq -1 ] ;}; then
min_x=$fold_coord
elif [[ $fold_axis == 'y' ]] && { [ $fold_coord -lt $min_y ] || [ $min_y -eq -1 ] ;}; then
min_y=$fold_coord
fi
for dot in ${!dots[@]}; do
dot_x=$(sed 's/,.*$//' <<< $dot)
dot_y=$(sed 's/^.*,//' <<< $dot)
if [[ $fold_axis == 'x' ]]; then
if [ $dot_x -gt $fold_coord ]; then
new_dots+=( "$((fold_coord*2-dot_x)),$dot_y" )
todel_dots+=( $dot )
fi
else
if [ $dot_y -gt $fold_coord ]; then
new_dots+=( "$dot_x,$((fold_coord*2-dot_y))" )
todel_dots+=( $dot )
fi
fi
done
for todel_dot in ${todel_dots[@]}; do
unset dots[$todel_dot]
done
for new_dot in ${new_dots[@]}; do
dots[$new_dot]=1
done
done
for j in $(seq 0 $min_y); do
for i in $(seq 0 $min_x); do
if [ ${dots["$i,$j"]} ]; then
echo -n '#'
else
echo -n ' '
fi
done
echo
done