Paste: aoc4
Author: | jon |
Mode: | factor |
Date: | Fri, 4 Dec 2020 09:56:25 |
Plain Text |
:: count-valid-passports ( validator: ( assoc -- ? ) -- n )
"/tmp/input" ascii file-lines { "" } split [
" " join " " split [ ":" split first2 ] H{ } map>assoc
validator call
] count ; inline
: p1 ( -- n )
[
{ "byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid" } swap keys
subset?
] count-valid-passports ;
: p2 ( -- n ) [
{
[ "byr" of string>number dup [ 1920 2002 between? ] when ]
[ "iyr" of string>number dup [ 2010 2020 between? ] when ]
[ "eyr" of string>number dup [ 2020 2030 between? ] when ]
[ "hgt" of {
{ [ "cm" ?tail ] [ 150 193 ] }
{ [ "in" ?tail ] [ 59 76 ] }
[ f f ]
} cond [ [ string>number ] 2dip pick [ between? ] [ 3drop f ] if ] [ nip ] if* ]
[ "hcl" of "#" ?head [ [ length 6 = ] [ "0123456789abcdef" subset? ] bi and ] [ drop f ] if ]
[ "ecl" of { "amb" "blu" "brn" "gry" "grn" "hzl" "oth" } in? ]
[ "pid" of [ length 9 = ] [ "0123456789" subset? ] bi and ]
} 1&& ] count-valid-passports ;
New Annotation