From e581bf63c493385c01291e988d3961363ec8d6a3 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 4 Dec 2020 01:20:11 -0500 Subject: Solve day 4 part 1 The read builtin returning 1 for EOF when the last line doesn't end with a newline tripped me up. Only a problem because I like to just copy the input and use pbpaste, I guess. --- 2020/day04-1.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2020/day04-1.sh (limited to '2020') diff --git a/2020/day04-1.sh b/2020/day04-1.sh new file mode 100644 index 0000000..afac196 --- /dev/null +++ b/2020/day04-1.sh @@ -0,0 +1,30 @@ +#!/bin/sh +valid=0 +validate() { + [ \ + -n "${byr}" -a \ + -n "${iyr}" -a \ + -n "${eyr}" -a \ + -n "${hgt}" -a \ + -n "${hcl}" -a \ + -n "${ecl}" -a \ + -n "${pid}" \ + ] && valid=$((valid + 1)) + 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