From bd144bd7e8395598cfc755625770dcb97753361d Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 2 Dec 2020 12:03:45 -0500 Subject: [PATCH] split each day into a separate source file --- 2020/Day01.cs | 58 ++++++++++++++++++++++++ 2020/Day02.cs | 62 +++++++++++++++++++++++++ 2020/DayTemplate.cs | 24 ++++++++++ 2020/Program.cs | 108 +++----------------------------------------- 4 files changed, 151 insertions(+), 101 deletions(-) create mode 100644 2020/Day01.cs create mode 100644 2020/Day02.cs create mode 100644 2020/DayTemplate.cs diff --git a/2020/Day01.cs b/2020/Day01.cs new file mode 100644 index 0000000..2910ef7 --- /dev/null +++ b/2020/Day01.cs @@ -0,0 +1,58 @@ +using System; +using static System.Console; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Xunit; + +namespace AdventOfCode { + + public class Day01 { + + static int Part1(List numbers) { + for (var i = 0; i < numbers.Count; i++) { + for (var j = i + 1; j < numbers.Count; j++) { + if (numbers[i] + numbers[j] == 2020) { + return numbers[i] * numbers[j]; + } + } + } + throw new ArgumentException("no pair of numbers summed to 2020"); + } + + static int Part2(List numbers) { + for (var i = 0; i < numbers.Count; i++) { + for (var j = i + 1; j < numbers.Count; j++) { + for (var k = j + 1; k < numbers.Count; k++) { + if (numbers[i] + numbers[j] + numbers[k] == 2020) { + return numbers[i] * numbers[j] * numbers[k]; + } + } + } + } + throw new ArgumentException("no triple of numbers summed to 2020"); + } + + [Fact] + public static void Test() { + var emptyInput = new List(); + var bogusInput = new List() { + 111, 222, 333, 444, 555, 666, 777, 888, 999 + }; + var exampleInput = new List() { + 1721, 979, 366, 299, 675, 1456 + }; + var testInput = Util.ReadInts(Util.RootDir + "day01.txt"); + + Assert.Throws(() => Part1(emptyInput)); + Assert.Throws(() => Part1(bogusInput)); + Assert.Equal(514579, Part1(exampleInput)); + Assert.Equal(355875, Part1(testInput)); + + Assert.Throws(() => Part2(emptyInput)); + Assert.Throws(() => Part2(bogusInput)); + Assert.Equal(241861950, Part2(exampleInput)); + Assert.Equal(140379120, Part2(testInput)); + } + } +} diff --git a/2020/Day02.cs b/2020/Day02.cs new file mode 100644 index 0000000..1bc562c --- /dev/null +++ b/2020/Day02.cs @@ -0,0 +1,62 @@ +using System; +using static System.Console; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Xunit; + +namespace AdventOfCode { + + public class Day02 { + + 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 Part1() { + return File.ReadAllLines(Util.RootDir + "day02.txt").Count(IsPasswordValid1); + } + + static int Part2() { + return File.ReadAllLines(Util.RootDir + "day02.txt").Count(IsPasswordValid2); + } + + [Fact] + public static void Test() { + Assert.True(IsPasswordValid1("1-3 a: abcde")); + Assert.False(IsPasswordValid1("1-3 b: cdefg")); + Assert.True(IsPasswordValid1("2-9 c: ccccccccc")); + Assert.Equal(603, Part1()); + + Assert.True(IsPasswordValid2("1-3 a: abcde")); + Assert.False(IsPasswordValid2("1-3 b: cdefg")); + Assert.False(IsPasswordValid2("2-9 c: ccccccccc")); + Assert.Equal(404, Part2()); + } + } +} diff --git a/2020/DayTemplate.cs b/2020/DayTemplate.cs new file mode 100644 index 0000000..b91aed5 --- /dev/null +++ b/2020/DayTemplate.cs @@ -0,0 +1,24 @@ +using System; +using static System.Console; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Xunit; + +namespace AdventOfCode { + + public class DayXX { + + static void Part1() { + } + + static void Part2() { + } + + [Fact(Skip = "template")] + public static void Test() { + Part1(); + Part2(); + } + } +} diff --git a/2020/Program.cs b/2020/Program.cs index 9d8bacc..5468704 100644 --- a/2020/Program.cs +++ b/2020/Program.cs @@ -7,114 +7,20 @@ using Xunit; namespace AdventOfCode { - public class Program { - - static string RootDir = + public class Util { + public static string RootDir = Environment.GetEnvironmentVariable("HOME") + "/src/adventofcode/2020/"; - static List ReadInts(string filename) { + public static List ReadInts(string filename) { string[] lines = File.ReadAllLines(filename); return lines.Select(int.Parse).ToList(); } + } - static int Day1Part1(List numbers) { - for (var i = 0; i < numbers.Count; i++) { - for (var j = i + 1; j < numbers.Count; j++) { - if (numbers[i] + numbers[j] == 2020) { - return numbers[i] * numbers[j]; - } - } - } - throw new ArgumentException("no pair of numbers summed to 2020"); - } - - static int Day1Part2(List numbers) { - for (var i = 0; i < numbers.Count; i++) { - for (var j = i + 1; j < numbers.Count; j++) { - for (var k = j + 1; k < numbers.Count; k++) { - if (numbers[i] + numbers[j] + numbers[k] == 2020) { - return numbers[i] * numbers[j] * numbers[k]; - } - } - } - } - throw new ArgumentException("no triple of numbers summed to 2020"); - } - - [Fact] - static void Day1Test() { - var emptyInput = new List(); - var bogusInput = new List() { - 111, 222, 333, 444, 555, 666, 777, 888, 999 - }; - var exampleInput = new List() { - 1721, 979, 366, 299, 675, 1456 - }; - var testInput = ReadInts(RootDir + "day01.txt"); - - Assert.Throws(() => Day1Part1(emptyInput)); - Assert.Throws(() => Day1Part1(bogusInput)); - Assert.Equal(514579, Day1Part1(exampleInput)); - Assert.Equal(355875, Day1Part1(testInput)); - - Assert.Throws(() => Day1Part2(emptyInput)); - Assert.Throws(() => Day1Part2(bogusInput)); - Assert.Equal(241861950, Day1Part2(exampleInput)); - 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()); - } - + public class Program { static void Main(string[] args) { - Day2Test(); + Day01.Test(); + Day02.Test(); } } }