From f3371fffba9b2857fce35a06942e758c08b4bfa7 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 4 Dec 2020 01:38:48 -0500 Subject: Solve day 4 part 2 --- 2020/day04-2.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2020/day04-2.sh (limited to '2020') 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 -- cgit 1.4.1