-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2020day4part2.linq
224 lines (183 loc) · 4.01 KB
/
adventOfCode2020day4part2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<Query Kind="Program" />
void Main()
{
var test = ReadMyFile(@"input4.txt");
//test.Dump();
var ports = ParsePassports(test);
//ports.Dump();
var count = 0;
foreach (var passport in ports)
{
if (passport.IsValid())
count++;
}
count.Dump();
}
private List<Passport> ParsePassports(List<string> lines)
{
var list = new List<Passport>();
var pass = new Passport();
foreach (var line in lines)
{
if (string.IsNullOrEmpty(line))
{
list.Add(pass);
pass = new Passport();
}
pass.FeedData(line);
}
list.Add(pass);
return list;
}
private List<string> ReadMyFile(string uri)
{
var newList = new List<string>();
string line;
System.IO.StreamReader reader = new StreamReader(uri);
while ((line = reader.ReadLine()) != null)
{
newList.Add(line);
}
return newList;
}
class Passport
{
const string byr = "byr";
const string iyr = "iyr";
const string eyr = "eyr";
const string hgt = "hgt";
const string hcl = "hcl";
const string ecl = "ecl";
const string pid = "pid";
const string cid = "cid";
List<(string, bool)> reqFields;
public int? Byr { get; set; }
public int? Iyr { get; set; }
public int? Eyr { get; set; }
public string Hgt { get; set; }
public string Hcl { get; set; }
public string Ecl { get; set; }
public int? Pid { get; set; }
public string Cid { get; set; }
public Passport()
{
reqFields = new List<(string, bool)>() { (byr, true), (iyr, true), (eyr, true), (hgt, true), (hcl, true), (ecl, true), (pid, true), (cid, false) };
}
public void FeedData(string line)
{
foreach (var value in reqFields)
{
if (line.Contains(value.Item1))
{
var str = line.Substring(line.IndexOf(value.Item1));
if (str.IndexOf(" ") > 0)
str = str.Substring(0, str.IndexOf(" "));
str = str.Replace(value.Item1, "").Substring(1);
SetData(str, value.Item1);
}
}
}
public bool IsValid()
{
foreach (var elem in reqFields.Where(w => w.Item2))
{
if (!CheckData(elem.Item1))
return false;
}
return true;
}
private bool CheckData(string field)
{
switch (field)
{
case byr:
return Byr != null;
case iyr:
return Iyr != null;
case eyr:
return Eyr != null;
case hgt:
return !string.IsNullOrEmpty(Hgt);
case hcl:
return !string.IsNullOrEmpty(Hcl);
case ecl:
return !string.IsNullOrEmpty(Ecl);
case pid:
return Pid != null;
case cid:
return !string.IsNullOrEmpty(Cid);
}
return false;
}
private int? DigitWithRange(string value, int digits, int min, int max)
{
if (int.TryParse(value, out int res))
{
if (value.Length != digits)
return null;
if (res > max || res < min)
return null;
return res;
}
return null;
}
private string CheckHeight(string value)
{
if (value.Contains("cm") && DigitWithRange(value.Replace("cm", ""), 3, 150, 193) != null)
return value;
if (value.Contains("in") && DigitWithRange(value.Replace("in", ""), 2, 59, 76) != null)
return value;
return null;
}
private string CheckHairColor(string value){
if(!value[0].Equals('#'))
return null;
if(value.Substring(1).Length!=6)
return null;
Regex r = new Regex("[^a-f0-9]$");
if (r.IsMatch(value.Substring(1)))
{
// validation failed
return null;
}
return value;
}
private string CheckEyeColor(string value)
{
var eyes = new List<string>() {"amb","blu","brn","gry","grn","hzl","oth"};
if(eyes.Contains(value))
return value;
return null;
}
private void SetData(string value, string field)
{
switch (field)
{
case byr:
Byr = DigitWithRange(value, 4, 1920, 2002);
break;
case iyr:
Iyr = DigitWithRange(value, 4, 2010, 2020);
break;
case eyr:
Eyr = DigitWithRange(value, 4, 2020, 2030);
break;
case hgt:
Hgt = CheckHeight(value);
break;
case hcl:
Hcl = CheckHairColor(value);
break;
case ecl:
Ecl = CheckEyeColor(value);
break;
case pid:
Pid = DigitWithRange(value, 9, 0, int.MaxValue);
break;
case cid:
Cid = value;
break;
}
}
}
// Define other methods and classes here