-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.linq
78 lines (65 loc) · 1.87 KB
/
23.linq
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
<Query Kind="Program">
<RuntimeVersion>5.0</RuntimeVersion>
</Query>
#load ".\shared"
void Main()
{
string inputString = Utils.ParseStrings("23.txt").First();
var p1Node = Solve(inputString, 100, inputString.Length);
var p2Node = Solve(inputString, 10_000_000, 1_000_000);
p1Node = Next(p1Node);
StringBuilder sb = new();
while (p1Node.Value != 1)
{
sb.Append(p1Node.Value);
p1Node = Next(p1Node);
}
sb.ToString().Dump("P1");
(Next(p2Node).Value * Next(Next(p2Node)).Value).Dump("P2");
}
LinkedListNode<long> Solve(string inputString, long totalMoves, long totalCups)
{
long[] inputs = inputString.Select(c => long.Parse(c.ToString())).ToArray();
long max = inputs.Max();
Dictionary<long, LinkedListNode<long>> lookup = new();
LinkedList<long> linkedList = new();
foreach (var n in inputs)
{
var node = linkedList.AddLast(n);
lookup.Add(n, node);
}
for (long i = max + 1; linkedList.Count < totalCups; i++)
{
var node = linkedList.AddLast(i);
lookup.Add(i, node);
}
max = linkedList.Count;
var current = linkedList.First;
for (int move = 1; move <= totalMoves; move++)
{
LinkedListNode<long>[] removed = new LinkedListNode<long>[3];
removed[0] = Next(current);
linkedList.Remove(Next(current));
removed[1] = Next(current);
linkedList.Remove(Next(current));
removed[2] = Next(current);
linkedList.Remove(Next(current));
long target = current.Value - 1;
while (removed.Select(lln => lln.Value).Contains(target) || target <= 0)
{
if (target == 0) target = max;
else target--;
}
var targetCup = lookup[target];
linkedList.AddAfter(targetCup, removed[2]);
linkedList.AddAfter(targetCup, removed[1]);
linkedList.AddAfter(targetCup, removed[0]);
current = Next(current);
}
return lookup[1];
}
LinkedListNode<long> Next(LinkedListNode<long> current)
{
if (current.Next != null) return current.Next;
return current.List.First;
}