DEV Community

loading...

Discussion on: Advent of Code 2020 Solution Megathread - Day 4: Passport Processing

Collapse
particleflux profile image
Stefan Linke

Warning this might be offensive to some :D
My Saturday was too long, so I created a code-golfed PHP version (for both parts)

<?$r=['byr):((19[2-9]\d|200[0-2]','iyr):((201\d|2020','eyr):((202\d|2030','hgt):(((?:1[5-8]\d|19[0-3])cm|(?:59|6\d|7[0-6])in','hcl):((#[\da-f]{6}','ecl):((amb|blu|brn|gry|grn|hzl|oth','pid):((\d{9}'];foreach(explode("

",file_get_contents('input'))as$p){$s=$t=1;for($i=0;$i<7;)if(!preg_match("/(?:^|\s)({$r[$i++]})(\s|$))?/",$p,$m))$s=$t=0;elseif(!($m[2]??0))$t=0;$x+=$s;$y+=$t;}echo"$x $y";
Enter fullscreen mode Exit fullscreen mode