day 9 part 1 solution
This commit is contained in:
parent
ece9f8cdd2
commit
0ba214e030
76
2020/Day09.cs
Normal file
76
2020/Day09.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using static System.Console;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Xunit;
|
||||
|
||||
namespace AdventOfCode {
|
||||
|
||||
public class Day09 {
|
||||
|
||||
static bool Valid(List<long> ints, int preambleSize, int target) {
|
||||
for (int i = target - preambleSize; i < target - 1; i++) {
|
||||
for (int j = i + 1; j < target; j++) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
if (ints[i] + ints[j] == ints[target]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static long FindInvalid(List<long> ints, int preambleSize) {
|
||||
for (int target = preambleSize; target < ints.Count(); target++) {
|
||||
if (!Valid(ints, preambleSize, target)) {
|
||||
return ints[target];
|
||||
}
|
||||
}
|
||||
throw new Exception("didn't find an invalid int");
|
||||
}
|
||||
|
||||
static long Part1() {
|
||||
string[] input = File.ReadAllLines(Util.RootDir + "day09.txt");
|
||||
List<long> ints = input.ToList().Select(long.Parse).ToList();
|
||||
return FindInvalid(ints, 25);
|
||||
}
|
||||
|
||||
static int Part2() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void Test() {
|
||||
string[] example =
|
||||
@"35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576".Split('\n');
|
||||
List<long> ints = example.ToList().Select(long.Parse).ToList();
|
||||
Assert.Equal(127, FindInvalid(ints, 5));
|
||||
|
||||
Assert.Equal(50047984, Part1());
|
||||
Assert.Equal(-1, Part2());
|
||||
}
|
||||
}
|
||||
}
|
1000
2020/day09.txt
Normal file
1000
2020/day09.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user