day 2 solution

This commit is contained in:
Colin McMillen 2020-12-02 11:18:16 -05:00
parent d4f34eb07c
commit c69ad1e76a
2 changed files with 1051 additions and 1 deletions

View File

@ -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();
}
}
}

1000
2020/day02.txt Normal file

File diff suppressed because it is too large Load Diff