-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-2.linq
87 lines (73 loc) · 2.16 KB
/
4-2.linq
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<Query Kind="Program" />
#load ".\shared"
void Main()
{
var strings = Utils.ParseStrings("4.txt");
int counter = 0;
Dictionary<string, string> fields = new Dictionary<string, string>();
foreach (var s in strings)
{
if (s == null || s.Length == 0)
{
if (Contains("byr") && InIntRange("byr", 1920, 2002) &&
Contains("iyr") && InIntRange("iyr", 2010, 2020) &&
Contains("eyr") && InIntRange("eyr", 2020, 2030) &&
Contains("hgt") && ValidHeight() &&
Contains("hcl") && ValidHairColor() &&
Contains("ecl") && ValidEyeColor() &&
Contains("pid") && ValidPassportId())
counter++;
fields = new Dictionary<string, string>();
}
else
{
string[] split = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);
foreach (var sp in split)
{
string[] split2 = sp.Split(':', StringSplitOptions.RemoveEmptyEntries);
fields.Add(split2[0], split2[1]);
}
}
}
$"P2: {counter}".Dump();
bool Contains(string field)
{
return fields.ContainsKey(field);
}
bool InIntRange(string field, int lower, int upper)
{
int value = 0;
bool valid = int.TryParse(fields[field], out value);
return valid && value >= lower && value <= upper;
}
bool ValidHeight()
{
string stringValue = fields["hgt"];
string numberPart = stringValue.Substring(0, stringValue.Length - 2);
string measurement = stringValue.Substring(stringValue.Length - 2);
int value = 0;
bool valid = int.TryParse(numberPart, out value);
if (measurement == "cm")
return valid && value >= 150 && value <= 193;
else return valid && value >= 59 && value <= 76;
}
bool ValidHairColor()
{
string stringValue = fields["hcl"];
if (stringValue.Length == 7 && stringValue[0] == '#' &&
stringValue.Substring(1).All(c => char.IsDigit(c) || c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'))
return true;
return false;
}
bool ValidEyeColor()
{
string sv = fields["ecl"];
string[] colors = new[] { "amb", "blu", "brn", "gry", "grn", "hzl", "oth" };
return colors.Contains(sv);
}
bool ValidPassportId()
{
string stringValue = fields["pid"];
return stringValue.Length == 9 && stringValue.All(c => char.IsNumber(c));
}
}