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

45 lines
847 B
Bash
Executable File

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