/
Day4.cs
134 lines (121 loc) · 3.27 KB
/
Day4.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace advent2020
{
class Day4
{
public static HashSet<string> validColors = new HashSet<string> { "amb", "blu", "brn", "gry", "grn", "hzl", "oth" };
public static Dictionary<string, Func<string, bool>> Validators = new Dictionary<string, Func<string, bool>> {
{"byr", x=> x.Length == 4 && int.Parse(x) >= 1920 && int.Parse(x) <= 2002},
{"iyr", x=> x.Length == 4 && int.Parse(x) >= 2010 && int.Parse(x) <= 2020 },
{"eyr", x=> x.Length == 4 && int.Parse(x) >= 2020 && int.Parse(x) <= 2030 },
{"hgt", ValidHeight},
{"hcl", ValidHaircolor},
{"ecl", x=> validColors.Contains(x)},
{"pid", x=> x.Length == 9 && x.All(char.IsDigit)}
};
private static bool ValidHaircolor(string s)
{
if (s.Length != 7)
{
return false;
}
if (s[0] != '#')
{
return false;
}
s = s.Substring(1, s.Length - 1);
if (s.All(x => char.IsDigit(x) || (x >= 'a' && x <= 'f')))
{
return true;
}
return false;
}
private static bool ValidHeight(string s)
{
if (s.Contains("in"))
{
s = s.Substring(0, s.Length - 2);
int h = int.Parse(s);
if (h >= 59 && h <= 76)
{
return true;
}
return false;
}
else if (s.Contains("cm"))
{
s = s.Substring(0, s.Length - 2);
int h = int.Parse(s);
if (h >= 150 && h <= 193)
{
return true;
}
return false;
}
else
{
return false;
}
}
public static void Run()
{
Console.WriteLine("Test data:");
Run("data/testdata4.txt");
Console.WriteLine("Answer data:");
Run("data/data4.txt");
Console.WriteLine("invalid data:");
Run("data/invalid4.txt");
Console.WriteLine("valid data:");
Run("data/valid4.txt");
}
static void Run(string filepath)
{
var lines = File.ReadAllLines(filepath);
var passports = Parse(lines);
var mandatory = new HashSet<string> { "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" };
int valid = 0;
foreach (var passport in passports)
{
if (mandatory.All(x => passport.ContainsKey(x)))
{
valid++;
}
}
Console.WriteLine($"valid passports: {valid}");
valid = 0;
foreach (var passport in passports)
{
if (Validators.All(v => passport.ContainsKey(v.Key) && v.Value(passport[v.Key])))
{
valid++;
}
}
Console.WriteLine($"B valid {valid}");
}
static List<Dictionary<string, string>> Parse(string[] lines)
{
int i = 0;
var passports = new List<Dictionary<string, string>>();
passports.Add(new Dictionary<string, string>());
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
{
i++;
passports.Add(new Dictionary<string, string>());
continue;
};
var pairs = line.Split(" ");
foreach (var pair in pairs)
{
var splitted = pair.Split(":");
passports[i].Add(splitted[0], splitted[1]);
}
}
return passports;
}
}
}