-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day10.cs
71 lines (58 loc) · 1.58 KB
/
Day10.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
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2020.Solvers;
public class Day10 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int len = input.Count((byte)'\n');
int[] nums = new int[len];
var reader = new SpanReader(input);
for (int i = 0; i < len; i++)
{
nums[i] = reader.ReadPosIntUntil('\n');
}
Array.Sort(nums);
int oneDiffs = 0;
int threeDiffs = 1; // there will always be a diff of three at the end, so start at 1
long ways0 = 0;
long ways1 = 0;
long ways2 = 1;
int prev0 = int.MinValue;
int prev1 = int.MinValue;
int prev2 = 0;
foreach (int num in nums)
{
// part 1
int diff = num - prev2;
if (diff == 1)
{
oneDiffs++;
}
else if (diff == 3)
{
threeDiffs++;
}
// part 2
long ways = ways2;
if (num - prev1 <= 3)
{
ways += ways1;
if (num - prev0 <= 3)
{
ways += ways0;
}
}
prev0 = prev1;
prev1 = prev2;
prev2 = num;
ways0 = ways1;
ways1 = ways2;
ways2 = ways;
}
int part1 = oneDiffs * threeDiffs;
long part2 = ways2;
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}