2020-11-30 16:27:07 +00:00
|
|
|
using System;
|
|
|
|
using static System.Console;
|
|
|
|
using System.Collections.Generic;
|
2020-12-01 16:04:50 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-11-30 16:27:07 +00:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace AdventOfCode {
|
|
|
|
|
|
|
|
public class Program {
|
|
|
|
|
2020-12-01 16:04:50 +00:00
|
|
|
static string RootDir =
|
|
|
|
Environment.GetEnvironmentVariable("HOME") + "/src/adventofcode/2020/";
|
|
|
|
|
|
|
|
static List<int> ReadInts(string filename) {
|
|
|
|
string[] lines = File.ReadAllLines(filename);
|
2020-12-01 18:08:02 +00:00
|
|
|
return lines.Select(int.Parse).ToList();
|
2020-12-01 16:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:34:34 +00:00
|
|
|
static int Day1Part1(List<int> 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<int> 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");
|
2020-11-30 16:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2020-12-01 15:34:34 +00:00
|
|
|
static void Day1Test() {
|
|
|
|
var emptyInput = new List<int>();
|
|
|
|
var bogusInput = new List<int>() {
|
|
|
|
111, 222, 333, 444, 555, 666, 777, 888, 999
|
|
|
|
};
|
|
|
|
var exampleInput = new List<int>() {
|
|
|
|
1721, 979, 366, 299, 675, 1456
|
|
|
|
};
|
2020-12-01 16:04:50 +00:00
|
|
|
var testInput = ReadInts(RootDir + "day1.txt");
|
2020-12-01 15:34:34 +00:00
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => Day1Part1(emptyInput));
|
|
|
|
Assert.Throws<ArgumentException>(() => Day1Part1(bogusInput));
|
|
|
|
Assert.Equal(514579, Day1Part1(exampleInput));
|
|
|
|
Assert.Equal(355875, Day1Part1(testInput));
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => Day1Part2(emptyInput));
|
|
|
|
Assert.Throws<ArgumentException>(() => Day1Part2(bogusInput));
|
|
|
|
Assert.Equal(241861950, Day1Part2(exampleInput));
|
|
|
|
Assert.Equal(140379120, Day1Part2(testInput));
|
2020-11-30 16:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Main(string[] args) {
|
2020-12-01 15:34:34 +00:00
|
|
|
Day1Test();
|
2020-11-30 16:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|