-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2020day2part2.linq
110 lines (83 loc) · 2.14 KB
/
adventOfCode2020day2part2.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
<Query Kind="Program" />
void Main()
{
var inputList = new List<string>();
inputList = GetSampleList().Dump();
var passwords=ParseStringsToPassword(inputList).Dump();
int counter=0;
foreach (var pw in passwords)
{
if(pw.IsValid())
counter++;
}
counter.Dump();
inputList=ReadMyFile(@"d:\input2.txt");
passwords = ParseStringsToPassword(inputList).Dump();
counter = 0;
foreach (var pw in passwords)
{
if (pw.IsValid())
counter++;
}
counter.Dump();
}
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;
}
private List<Password> ParseStringsToPassword(List<string> strs){
var list=new List<Password>();
foreach (var str in strs)
{
string substring=str;
var pw = new Password();
int location=substring.IndexOf("-");
var minString = substring.Substring(0, location);
substring=substring.Substring(location+1);
pw.minOccurence = int.Parse(minString);
location=substring.IndexOf(" ");
var maxOc=substring.Substring(0, location);
location=substring.IndexOf(" ");
pw.MaxOccurence=int.Parse(maxOc);
substring=substring.Substring(substring.IndexOf(" "));
pw.Character=substring.Substring(0,substring.IndexOf(":")).Replace(" ","");
substring=substring.Substring(str.IndexOf(" ")+1);
pw.PwString=str.Substring(str.IndexOf(":")+1).Replace(" ","");
list.Add(pw);
}
return list;
}
private List<string> GetSampleList()
{
var newList = new List<string>();
newList.Add("1-3 a: abcde");
newList.Add("1-3 b: cdefg");
newList.Add("2-9 c: ccccccccc");
return newList;
}
class Password
{
public int minOccurence { get; set; }
public int MaxOccurence { get; set; }
public string Character { get; set; }
public string PwString { get; set; }
public bool IsValid(){
bool hit=false;
if(PwString[minOccurence-1].ToString().Equals(Character))
hit=true;
if(PwString[MaxOccurence-1].ToString().Equals(Character)){
if(hit)
return false;
hit=true;
}
return hit;
}
}
// Define other methods and classes here