-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolve.awk
47 lines (40 loc) · 979 Bytes
/
solve.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
BEGIN {
count_1 = 0
count_2 = 0;
split("byr iyr eyr hgt hcl ecl pid", required)
}
function validate_1() {
for (f in required) {
if (!(required[f] in cur)) {
return 0;
};
}
return 1;
}
function validate_2() {
if (cur["byr"] > 2020 || cur["byr"] < 1920 \
|| cur["eyr"] > 2030 || cur["eyr"] < 2020 \
|| cur["iyr"] > 2020 || cur["iyr"] < 2010 \
|| cur["hcl"] !~ /#[0-9a-f]{6}$/ \
|| cur["ecl"] !~ /(amb|blu|brn|gry|grn|hzl|oth)$/ \
|| cur["pid"] !~ /[0-9]{9}$/) return 0;
h = cur["hgt"];
if (h ~ /[1-9][0-9]in/) return h >= 59 && h <= 76;
if (h ~ /1[0-9]{2}cm/) return h >= 150 && h <= 193;
}
function validate() {
count_1 += validate_1();
count_2 += validate_2();
split("", cur);
}
/^$/ { validate(); }
/.+/ {
for (i=1; i<=NF; i++) {
split($(i), p, ":");
cur[p[1]] = p[2];
}
}
END {
validate();
print count_1 " " count_2
}