summary refs log tree commit diff homepage
path: root/2020
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-04 01:38:48 -0500
committerJune McEnroe <june@causal.agency>2020-12-04 01:38:48 -0500
commitf3371fffba9b2857fce35a06942e758c08b4bfa7 (patch)
tree542abf4a92bab7ca97bc29953a95b965f8565f79 /2020
parentSolve day 4 part 1 (diff)
downloadaoc-f3371fffba9b2857fce35a06942e758c08b4bfa7.tar.gz
aoc-f3371fffba9b2857fce35a06942e758c08b4bfa7.zip
Solve day 4 part 2
Diffstat (limited to '2020')
-rw-r--r--2020/day04-2.sh42
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