43 lines
979 B
D
43 lines
979 B
D
import std.stdio;
|
|
import std.file;
|
|
import std.array;
|
|
import std.uni : isWhite;
|
|
import std.conv;
|
|
import std.regex;
|
|
|
|
void main() {
|
|
string contents = readText("input");
|
|
auto lines = contents.split("\n\n");
|
|
int good = 0;
|
|
|
|
foreach(i, line; lines) {
|
|
auto parts = line.split!(isWhite);
|
|
|
|
string[] items = [ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ];
|
|
int[string] totals;
|
|
foreach(y, item; items)
|
|
totals[item] = 0;
|
|
|
|
foreach(x, part; parts) {
|
|
auto fields = part.split(":");
|
|
if(fields.length < 2)
|
|
continue;
|
|
|
|
if(!(fields[0] in totals))
|
|
continue;
|
|
|
|
if(part.matchAll(ctRegex!(r"^(pid:\d{9}|byr:(19[2-9]\d|200[0-2])|iyr:20(1\d|20)|eyr:20(2\d|30)|hgt:((59|6\d|7[0-6])in|(1[5-8]\d|19[0-3])cm)|hcl:#[0-9a-f]{6}|ecl:(amb|blu|brn|gry|grn|hzl|oth)|pid:\d{9}|cid:.*)$")))
|
|
totals[fields[0]]++;
|
|
}
|
|
|
|
int sum = 0;
|
|
foreach(z, total; totals)
|
|
sum += total;
|
|
|
|
if(sum == items.length)
|
|
good++;
|
|
}
|
|
|
|
writefln("There are %d valid passports.", good);
|
|
}
|