#!/usr/bin/env tclsh proc check {} { global field valid foreach f {byr iyr eyr hgt hcl ecl pid} { if {! [info exists field($f)]} return set v $field($f) switch -- $f { byr { if {! [string match {[0-9][0-9][0-9][0-9]} $v]} return if {$v < 1920 || $v > 2002} return } iyr { if {! [string match {[0-9][0-9][0-9][0-9]} $v]} return if {$v < 2010 || $v > 2020} return } eyr { if {! [string match {[0-9][0-9][0-9][0-9]} $v]} return if {$v < 2020 || $v > 2030} return } hgt { if {[regexp {^([0-9]+)cm$} $v -> cm]} { if {$cm < 150 || $cm > 193} return } elseif {[regexp {^([0-9]+)in$} $v -> in]} { if {$in < 59 || $in > 76} return } else return } hcl { if {! [regexp {^#[0-9a-f]{6}$} $v]} return } ecl { if {$v ni {amb blu brn gry grn hzl oth}} return } pid { if {! [regexp {^[0-9]{9}$} $v]} return } } } incr valid } set valid 0 while {[gets stdin line] >= 0} { if {$line eq ""} { check array unset field continue } foreach f [split $line { }] { set field([string range $f 0 2]) [string range $f 4 end] } } check puts $valid