59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
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<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 Part2(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");
|
|
}
|
|
|
|
[Fact]
|
|
public static void Test() {
|
|
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
|
|
};
|
|
var testInput = Util.ReadInts(Util.RootDir + "day01.txt");
|
|
|
|
Assert.Throws<ArgumentException>(() => Part1(emptyInput));
|
|
Assert.Throws<ArgumentException>(() => Part1(bogusInput));
|
|
Assert.Equal(514579, Part1(exampleInput));
|
|
Assert.Equal(355875, Part1(testInput));
|
|
|
|
Assert.Throws<ArgumentException>(() => Part2(emptyInput));
|
|
Assert.Throws<ArgumentException>(() => Part2(bogusInput));
|
|
Assert.Equal(241861950, Part2(exampleInput));
|
|
Assert.Equal(140379120, Part2(testInput));
|
|
}
|
|
}
|
|
}
|