diff --git a/day16/Makefile b/day16/Makefile index be9744d..a1d3261 100644 --- a/day16/Makefile +++ b/day16/Makefile @@ -20,7 +20,7 @@ ex1: @#$(TIME) ex1-c 2020 < $(INPUT) 2>&1 ex2: - @#$(TIME) ex1.bash 30000000 < $(INPUT) 2>&1 + @$(TIME) ex2.bash < $(INPUT) 2>&1 @#$(TIME) ex1-c 30000000 < $(INPUT) 2>&1 clean: diff --git a/day16/TEST2.txt b/day16/TEST2.txt new file mode 100644 index 0000000..b48bf37 --- /dev/null +++ b/day16/TEST2.txt @@ -0,0 +1,11 @@ +class: 0-1 or 4-19 +row: 0-5 or 8-19 +seat: 0-13 or 16-19 + +your ticket: +11,12,13 + +nearby tickets: +3,9,18 +15,1,5 +5,14,9 diff --git a/day16/ex1.bash b/day16/ex1.bash index 270462d..31fc468 100755 --- a/day16/ex1.bash +++ b/day16/ex1.bash @@ -20,6 +20,7 @@ while read -r line; do elif [[ $line != "" ]]; then if ((state == 2)); then for i in ${line//,/ }; do + # shellcheck disable=SC2100 [[ ! -v valid[$i] ]] && res=res+i done fi diff --git a/day16/ex2.bash b/day16/ex2.bash new file mode 100755 index 0000000..560d3a8 --- /dev/null +++ b/day16/ex2.bash @@ -0,0 +1,90 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 16/game 2. + +CMD=${0##*/} +shopt -s extglob + +declare -a valid=() myticket=() keys=() values=() match=() +declare -A position=() +declare -i state=0 res=1 curkey=0 curticket=0 + +while read -r line; do + if [[ $line =~ ^([a-z ]+)\:\ ([0-9]+)-([0-9]+)([a-z ]+)([0-9]+)-([0-9]+)$ ]]; then + # valid ranges + keys[$curkey]="${BASH_REMATCH[1]}" + n1=$(seq -s" " "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") + n2=$(seq -s" " "${BASH_REMATCH[5]}" "${BASH_REMATCH[6]}") + + for i in $n1 $n2; do + valid[$i]=1 + done + values[$curkey]="${n1[*]} ${n2[*]}" + ((curkey++)) + + elif [[ $line =~ (your ticket:|nearby tickets) ]]; then + # next section + ((state++)) + elif [[ $line =~ [0-9,]+ ]]; then + # shellcheck disable=SC2206 + numbers=( ${line//,/ } ) + case $state in + 1) # my ticket + # shellcheck disable=SC2206 + myticket=( ${numbers[@]} ) + # initialize possible matches array: all + for ((i=0; i