Compare commits

...

2 Commits

Author SHA1 Message Date
2ed395e1f3 rename day1.txt -> day01.txt 2020-12-02 11:27:46 -05:00
c69ad1e76a day 2 solution 2020-12-02 11:18:16 -05:00
3 changed files with 1051 additions and 1 deletions

View File

@ -63,8 +63,58 @@ namespace AdventOfCode {
Assert.Equal(140379120, Day1Part2(testInput)); 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) { static void Main(string[] args) {
Day1Test(); Day2Test();
} }
} }
} }

1000
2020/day02.txt Normal file

File diff suppressed because it is too large Load Diff