Day 16/part 1: bash version

This commit is contained in:
2021-01-06 13:32:56 +01:00
parent 0f962b6f83
commit 88f13ab3a6
5 changed files with 410 additions and 0 deletions

31
day16/ex1.bash Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 15/games 1 and 2.
CMD=${0##*/}
#shopt -s extglob
declare -A valid=()
declare -i state=0 res=0
while read -r line; do
if [[ $line =~ ^([a-z :]+)([0-9]+)-([0-9]+)([a-z ]+)([0-9]+)-([0-9]+)$ ]]; then
n1=$(seq "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}")
n2=$(seq "${BASH_REMATCH[5]}" "${BASH_REMATCH[6]}")
for i in $n1 $n2; do
valid[$i]=1
done
elif [[ $line =~ (your ticket:|nearby tickets) ]]; then
state=state+1
elif [[ $line != "" ]]; then
if ((state == 2)); then
for i in ${line//,/ }; do
[[ ! -v valid[$i] ]] && res=res+i
done
fi
fi
done
printf "%s : res=%d\n" "$CMD" "$res"
exit 0