-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day01.cs
69 lines (57 loc) · 1.86 KB
/
Day01.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
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
namespace AdventOfCode.CSharp.Y2018.Solvers;
public class Day01 : ISolver
{
public class Frequency
{
public int Value { get; set; }
public int Index { get; set; }
public int ModTotal { get; set; }
}
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int freqIndex = 0;
int freqTotal = 0;
var freqs = new List<Frequency>();
var reader = new SpanReader(input);
while (!reader.Done)
{
freqs.Add(new Frequency { Value = freqTotal, Index = freqIndex });
freqIndex++;
int mul = reader.Peek() == '-' ? -1 : 1;
reader.SkipLength(1);
freqTotal += mul * reader.ReadPosIntUntil('\n');
}
foreach (Frequency freq in freqs)
{
int mod = freq.Value % freqTotal;
freq.ModTotal = mod < 0 ? mod + freqTotal : mod;
}
// sort by mods first, then by value
freqs.Sort((a, b) => a.ModTotal != b.ModTotal
? a.ModTotal.CompareTo(b.ModTotal)
: a.Value.CompareTo(b.Value));
var prev = new Frequency { ModTotal = -1 };
int minDiff = int.MaxValue;
int minIndex = int.MaxValue;
int minFreq = 0;
foreach (Frequency freq in freqs)
{
if (freq.ModTotal == prev.ModTotal)
{
int diff = freq.Value - prev.Value;
if (diff < minDiff || (diff == minDiff && prev.Index < minIndex))
{
minDiff = diff;
minIndex = prev.Index;
minFreq = freq.Value;
}
}
prev = freq;
}
solution.SubmitPart1(freqTotal);
solution.SubmitPart2(minFreq);
}
}