✨ 2 stars 🎉!
This commit is contained in:
parent
41fcc43446
commit
6a4a72f1f1
1147
2020/4/input
Normal file
1147
2020/4/input
Normal file
File diff suppressed because it is too large
Load Diff
39
2020/4/validate.d
Normal file
39
2020/4/validate.d
Normal file
@ -0,0 +1,39 @@
|
||||
import std.stdio;
|
||||
import std.file;
|
||||
import std.array;
|
||||
import std.uni : isWhite;
|
||||
|
||||
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;
|
||||
|
||||
totals[fields[0]]++;
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
foreach(z, total; totals)
|
||||
sum += total;
|
||||
|
||||
if(sum == items.length)
|
||||
good++;
|
||||
}
|
||||
|
||||
writefln("There are %d valid passports.", good);
|
||||
}
|
42
2020/4/validate2.d
Normal file
42
2020/4/validate2.d
Normal file
@ -0,0 +1,42 @@
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user