diff options
Diffstat (limited to '2020')
-rw-r--r-- | 2020/day04-2.sh | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/2020/day04-2.sh b/2020/day04-2.sh new file mode 100644 index 0000000..5b3c2d3 --- /dev/null +++ b/2020/day04-2.sh @@ -0,0 +1,42 @@ +#!/bin/sh +valid=0 +check() { + [ \ + "${#byr}" -eq 4 -a "${byr}" -ge 1920 -a "${byr}" -le 2002 -a \ + "${#iyr}" -eq 4 -a "${iyr}" -ge 2010 -a "${iyr}" -le 2020 -a \ + "${#eyr}" -eq 4 -a "${eyr}" -ge 2020 -a "${eyr}" -le 2030 \ + ] || return 0 + case "${hgt}" in + (*cm) [ "${hgt%cm}" -ge 150 -a "${hgt%cm}" -le 193 ] || return 0 ;; + (*in) [ "${hgt%in}" -ge 59 -a "${hgt%in}" -le 76 ] || return 0 ;; + (*) return 0 ;; + esac + echo "${hcl}" | egrep -q '^#[0-9a-f]{6}$' || return 0 + case "${ecl}" in + (amb|blu|brn|gry|grn|hzl|oth) ;; + (*) return 0 ;; + esac + [ "${#pid}" -eq 9 ] || return 0 + return 1 +} +validate() { + check + valid=$((valid + $?)) + unset byr iyr eyr hgt hcl ecl pid cid +} +while :; do + read -r line + last=$? + if [ -n "${line}" ]; then + for pair in $line; do + var=${pair%:*} + val=${pair#*:} + eval "${var}=${val}" + done + else + validate + fi + [ $last -ne 0 ] && break +done +validate +echo $valid |