-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
74 lines (66 loc) · 2.02 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
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
using AdventOfCode.Common;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2020.Day13
{
internal class Solution
{
private const string OutOfService = "x";
private readonly long _timestamp;
private readonly (int Offset, int Id)[] _buses;
public Solution(IReadOnlyList<string> input)
{
_timestamp = Convert.ToInt64(input[0]);
_buses = (
from bus in input[1].Split(",").Indexed()
where bus.Value != OutOfService
select (bus.Index, int.Parse(bus.Value))
).ToArray();
}
public long PartOne()
{
var times =
from bus in _buses
let time = (bus.Id - _timestamp % bus.Id) % bus.Id
orderby time
select bus.Id * time;
return times.First();
}
public long PartTwo()
{
var busIds = (from bus in _buses select (long) bus.Id).ToArray();
var offsets = (from bus in _buses select (long) bus.Id - bus.Offset).ToArray();
return ChineseRemainderTheorem.Solve(busIds, offsets);
}
}
/// <summary>
/// Source: https://rosettacode.org/wiki/Chinese_remainder_theorem#C.23
/// </summary>
internal static class ChineseRemainderTheorem
{
public static long Solve(long[] n, long[] a)
{
var prod = n.Product();
var sm = 0L;
for (var i = 0; i < n.Length; i++)
{
var p = prod / n[i];
sm += a[i] * ModularMultiplicativeInverse(p, n[i]) * p;
}
return sm % prod;
}
private static long ModularMultiplicativeInverse(long a, long mod)
{
var b = a % mod;
for (var x = 1; x < mod; x++)
{
if (b * x % mod == 1)
{
return x;
}
}
return 1;
}
}
}