With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you.
Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input).
Read the full puzzle.
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Y2020.Day09;
[ProblemName("Encoding Error")]
class Solution : Solver {
IEnumerable<int> Range(int min, int lim) => Enumerable.Range(min, lim - min);
public object PartOne(string input) {
var numbers = input.Split("\n").Select(long.Parse).ToArray();
bool Mismatch(int i) => (
from j in Range(i - 25, i)
from k in Range(j + 1, i)
select numbers[j] + numbers[k]
).All(sum => sum != numbers[i]);
return numbers[Range(25, input.Length).First(Mismatch)];
}
public object PartTwo(string input) {
var d = (long)PartOne(input);
var lines = input.Split("\n").Select(long.Parse).ToList();
foreach (var j in Range(0, lines.Count)) {
var s = lines[j];
foreach (var k in Range(j + 1, lines.Count)) {
s += lines[k];
if (s > d) {
break;
} else if (s == d) {
var range = lines.GetRange(j, k - j + 1);
return range.Min() + range.Max();
}
}
}
throw new Exception();
}
}
Please ☆ my repo if you like it!