-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
49 lines (41 loc) · 1.27 KB
/
Solution.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
using AdventOfCode.Common;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2020.Day10
{
internal class Solution
{
private readonly IReadOnlyCollection<int> _adapters;
public Solution(IEnumerable<string> input)
{
_adapters = input.Select(int.Parse).ToList();
}
public long PartOne()
{
var differences = _adapters
.Append(0)
.Append(_adapters.Max() + 3)
.OrderBy(x => x)
.Pairwise()
.GroupBy(x => x.Item2 - x.Item1)
.ToDictionary(g => g.Key, g => g.LongCount());
return differences[1] * differences[3];
}
public long PartTwo()
{
var cache = new Dictionary<int, long>
{
{_adapters.Max(), 1L}
};
foreach (var adapter in _adapters.Append(0).OrderByDescending(x => x).Skip(1))
{
var count = Enumerable.Range(1, 3)
.Select(delta => adapter + delta)
.Select(x => cache.GetValueOrDefault(x, 0L))
.Sum();
cache[adapter] = count;
}
return cache[0];
}
}
}