-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.cs
96 lines (81 loc) · 2.6 KB
/
Day9.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
namespace aoc2020
{
public class Day9 : AocBase
{
public Day9()
{
Console.WriteLine("Day 9");
Console.WriteLine("Part 1");
const int preamble = 25;
var input = LoadInput<ulong>(@"input\day9.txt");
var value = Part1(input, preamble);
if (value == null)
{
Console.WriteLine("something went wrong.");
return;
}
Part2(input, value.Value);
}
private void Part2(IEnumerable<ulong> input, ulong value)
{
for (var i = 0; i < input.Count(); i++)
{
for (var j = 1; j < input.Count(); j++)
{
if (i == j)
{
continue;
}
var subset = input.Skip(i).Take(j);
var sum = subset.Aggregate((a, b) => a + b);
if (sum == value) {
subset = subset.OrderBy(a => a);
var low = subset.First();
var high = subset.Last();
Console.WriteLine($"low - {low} ");
Console.WriteLine($"high - {high} ");
Console.WriteLine($"result - {low+high} ");
return;
}
}
}
}
private ulong? Part1(IEnumerable<ulong> input, int preamble)
{
for (var i = preamble; i < input.Count(); i++)
{
var subset = input.Skip(i - preamble).Take(preamble);
var current = input.Skip(i).Take(1).FirstOrDefault();
if (!IsMatch(current, subset.ToArray()))
{
Console.WriteLine(current);
return current;
}
}
return null;
}
private bool IsMatch(ulong current, ulong[] subset)
{
for (var i = 0; i < subset.Length; i++)
{
for (var j = 0; j < subset.Length; j++)
{
if (i == j)
{
continue;
}
var val1 = subset[i];
var val2 = subset[j];
if (val1 + val2 == current)
{
return true;
}
}
}
return false;
}
}
}