-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.cs
More file actions
60 lines (52 loc) · 1.51 KB
/
Solver.cs
File metadata and controls
60 lines (52 loc) · 1.51 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace dotnet.day15
{
public class Disc
{
readonly int _number;
readonly int _size;
readonly int _initialPosition;
public Disc(int number, int size, int initialPosition)
{
_number = number;
_size = size;
_initialPosition = initialPosition;
}
public bool IsOpen(int t)
{
return (_initialPosition + t + _number) % _size == 0;
}
}
public class Solver
{
public int Solve(IEnumerable<string> input)
{
return Solve(CreateDiscsFromInput(input));
}
IEnumerable<Disc> CreateDiscsFromInput(IEnumerable<string> input)
{
foreach (var s in input)
{
var number = int.Parse(s.Substring(6, 1));
var size = int.Parse(s.Substring(12, 2).TrimEnd(' '));
var position = int.Parse(s.Substring(s.IndexOf("ion ") + 4).TrimEnd('.'));
yield return new Disc(number, size, position);
}
}
internal int Solve(IEnumerable<Disc> discs)
{
var t = 0;
while (true)
{
if (discs.All(d => d.IsOpen(t)))
return t;
t++;
}
}
}
}