Repository navigation
Expand file tree
/
Copy pathDay13.cs
More file actions
95 lines (76 loc) · 2.2 KB
/
Copy pathDay13.cs
File metadata and controls
95 lines (76 loc) · 2.2 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public static class Day13
{
public static decimal GetTheThing(string path)
{
var lines = File.ReadAllLines(path);
var time = 0m;
var buses = lines
.Last()
.Split(',')
.ToArray();
Tuple<int, int>? thing = null;
for (int i = 0; i < buses.Length; i++)
{
var bus = buses[i];
if (bus == "x")
continue;
var number = Convert.ToInt32(bus);
if (thing == null || thing.Item2 < number)
{
thing = new Tuple<int, int>(i, number);
}
}
time += Convert.ToInt32(thing!.Item2);
Console.WriteLine(time);
var iters = 0;
while (true)
{
var timeCounter = time - thing.Item1;
// Console.WriteLine(timeCounter);
for (int i = 0; i < buses.Length; i++)
{
iters++;
var bus = buses[i];
if (bus == "x")
{
timeCounter++;
continue;
}
var number = Convert.ToInt32(bus);
if (timeCounter % number != 0)
{
break;
}
if (i == buses.Length - 1)
{
Console.WriteLine(iters);
return timeCounter - i;
}
timeCounter++;
}
time += Convert.ToInt32(thing.Item2);
}
}
public static int GetBestBus(string path)
{
var lines = File.ReadAllLines(path);
var time = Convert.ToDouble(lines.First());
var buses = lines
.Last()
.Split(',')
.Where(x => int.TryParse(x, out var y))
.Select(x => Convert.ToDouble(x));
var times = buses
.Select(x => Tuple.Create(x, Math.Ceiling(time / x) * x))
.OrderBy(x => x.Item2)
.ToList();
foreach (var t in times)
{
var x = t.Item2 - time;
if (x < 0)
continue;
return (int)(t.Item1 * x);
}
return 0;
}
}