|
|
@ -63,8 +63,58 @@ namespace AdventOfCode { |
|
|
|
Assert.Equal(140379120, Day1Part2(testInput)); |
|
|
|
} |
|
|
|
|
|
|
|
static bool IsPasswordValid1(string spec) { |
|
|
|
string[] tokens = spec.Split(' '); |
|
|
|
string[] numbers = tokens[0].Split('-'); |
|
|
|
int min = int.Parse(numbers[0]); |
|
|
|
int max = int.Parse(numbers[1]); |
|
|
|
char required = tokens[1][0]; |
|
|
|
string password = tokens[2]; |
|
|
|
|
|
|
|
int count = 0; |
|
|
|
for (int i = 0; i < password.Length; i++) { |
|
|
|
if (password[i] == required) { |
|
|
|
count++; |
|
|
|
} |
|
|
|
} |
|
|
|
return min <= count && count <= max; |
|
|
|
} |
|
|
|
|
|
|
|
static bool IsPasswordValid2(string spec) { |
|
|
|
string[] tokens = spec.Split(' '); |
|
|
|
string[] numbers = tokens[0].Split('-'); |
|
|
|
int lowIndex = int.Parse(numbers[0]) - 1; |
|
|
|
int highIndex = int.Parse(numbers[1]) - 1; |
|
|
|
char required = tokens[1][0]; |
|
|
|
string password = tokens[2]; |
|
|
|
return |
|
|
|
(password[lowIndex] == required && password[highIndex] != required) || |
|
|
|
(password[lowIndex] != required && password[highIndex] == required); |
|
|
|
} |
|
|
|
|
|
|
|
static int Day2Part1() { |
|
|
|
return File.ReadAllLines(RootDir + "day02.txt").Count(IsPasswordValid1); |
|
|
|
} |
|
|
|
|
|
|
|
static int Day2Part2() { |
|
|
|
return File.ReadAllLines(RootDir + "day02.txt").Count(IsPasswordValid2); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
static void Day2Test() { |
|
|
|
Assert.True(IsPasswordValid1("1-3 a: abcde")); |
|
|
|
Assert.False(IsPasswordValid1("1-3 b: cdefg")); |
|
|
|
Assert.True(IsPasswordValid1("2-9 c: ccccccccc")); |
|
|
|
Assert.Equal(603, Day2Part1()); |
|
|
|
|
|
|
|
Assert.True(IsPasswordValid2("1-3 a: abcde")); |
|
|
|
Assert.False(IsPasswordValid2("1-3 b: cdefg")); |
|
|
|
Assert.False(IsPasswordValid2("2-9 c: ccccccccc")); |
|
|
|
Assert.Equal(404, Day2Part2()); |
|
|
|
} |
|
|
|
|
|
|
|
static void Main(string[] args) { |
|
|
|
Day1Test(); |
|
|
|
Day2Test(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |