-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
56 lines (50 loc) · 2.02 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace Day2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1. Number of passwords that comply with the policies: " + PassTrueOrNotNum(File.ReadAllLines("source.txt")));
Console.WriteLine("2. Number of passwords that comply with the new intepretation of the policies: " + PassTrueOrNotIndex(File.ReadAllLines("source.txt")));
}
static int PassTrueOrNotNum(IEnumerable<string> list)
{
int goodPass = 0;
for (int i = 0; i < list.Count(); i++)
{
List<string> split = list.ElementAt(i).Split(':').ToList();
split = new List<string>() { split[0].Split('-').ToList()[0], split[0].Split('-')[1].Split(' ')[0], split[0].Split(' ')[1], split[1] };
int min = int.Parse(split[0]);
int max = int.Parse(split[1]);
char must = char.Parse(split[2]);
int goodLetter = split[3].Count(x => x == must);
if (goodLetter >= min && goodLetter <= max)
{
goodPass++;
}
}
return goodPass;
}
static int PassTrueOrNotIndex(IEnumerable<string> list)
{
int goodPass = 0;
for (int i = 0; i < list.Count(); i++)
{
List<string> split = list.ElementAt(i).Split(':').ToList();
split = new List<string>() { split[0].Split('-').ToList()[0], split[0].Split('-')[1].Split(' ')[0], split[0].Split(' ')[1], split[1] };
int firstInd = int.Parse(split[0]);
int secondInd = int.Parse(split[1]);
char must = char.Parse(split[2]);
if (split[3][firstInd] == must ^ split[3][secondInd] == must)
{
goodPass++;
}
}
return goodPass;
}
}
}