ints -> longs

This commit is contained in:
Colin McMillen 2020-12-09 08:42:04 -05:00
parent 0ba214e030
commit 0cad16b5a4

View File

@ -10,13 +10,13 @@ namespace AdventOfCode {
public class Day09 { public class Day09 {
static bool Valid(List<long> ints, int preambleSize, int target) { static bool Valid(List<long> longs, int preambleSize, int target) {
for (int i = target - preambleSize; i < target - 1; i++) { for (int i = target - preambleSize; i < target - 1; i++) {
for (int j = i + 1; j < target; j++) { for (int j = i + 1; j < target; j++) {
if (i == j) { if (i == j) {
continue; continue;
} }
if (ints[i] + ints[j] == ints[target]) { if (longs[i] + longs[j] == longs[target]) {
return true; return true;
} }
} }
@ -24,10 +24,10 @@ namespace AdventOfCode {
return false; return false;
} }
static long FindInvalid(List<long> ints, int preambleSize) { static long FindInvalid(List<long> longs, int preambleSize) {
for (int target = preambleSize; target < ints.Count(); target++) { for (int target = preambleSize; target < longs.Count(); target++) {
if (!Valid(ints, preambleSize, target)) { if (!Valid(longs, preambleSize, target)) {
return ints[target]; return longs[target];
} }
} }
throw new Exception("didn't find an invalid int"); throw new Exception("didn't find an invalid int");
@ -35,8 +35,8 @@ namespace AdventOfCode {
static long Part1() { static long Part1() {
string[] input = File.ReadAllLines(Util.RootDir + "day09.txt"); string[] input = File.ReadAllLines(Util.RootDir + "day09.txt");
List<long> ints = input.ToList().Select(long.Parse).ToList(); List<long> longs = input.ToList().Select(long.Parse).ToList();
return FindInvalid(ints, 25); return FindInvalid(longs, 25);
} }
static int Part2() { static int Part2() {
@ -66,8 +66,8 @@ namespace AdventOfCode {
277 277
309 309
576".Split('\n'); 576".Split('\n');
List<long> ints = example.ToList().Select(long.Parse).ToList(); List<long> longs = example.ToList().Select(long.Parse).ToList();
Assert.Equal(127, FindInvalid(ints, 5)); Assert.Equal(127, FindInvalid(longs, 5));
Assert.Equal(50047984, Part1()); Assert.Equal(50047984, Part1());
Assert.Equal(-1, Part2()); Assert.Equal(-1, Part2());