Files
advent-of-code-2021/13/01.sh
2021-12-13 14:09:12 +01:00

45 lines
764 B
Bash
Executable File

#!/bin/bash
declare -A dots
folds=()
while read line; do
if [[ $line == *','* ]]; then
dots[$line]=1
else
folds+=( ${line#fold along } )
fi
done
fold=${folds[0]}
fold_axis=${fold%=*}
fold_coord=${fold#*=}
new_dots=()
todel_dots=()
for dot in ${!dots[@]}; do
dot_x=${dot%,*}
dot_y=${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
echo ${#dots[@]}