Skip to content

Commit 1fa201e

Browse files
committed
23 speedup
1 parent c1d0557 commit 1fa201e

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

2020/Day23/Solution.cs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,48 @@ public object PartTwo(string input) {
2929
}
3030

3131
class Cups {
32-
Cup currentCup;
33-
public Dictionary<long, Cup> cupsByLabel = new Dictionary<long, Cup>();
34-
int maxLabel;
32+
int currentCup;
33+
public int[] next;
3534

3635
public Cups(string input, int maxLabel) {
37-
this.maxLabel = maxLabel;
36+
next = Enumerable.Range(1, maxLabel + 1).ToArray();
37+
next[0] = -1;
3838

39-
var numbers =
40-
input.ToCharArray().Select(v => int.Parse(v.ToString()))
41-
.Concat(Enumerable.Range(input.Length + 1, maxLabel - input.Length))
42-
.Select(v => new Cup(v))
43-
.ToArray();
44-
45-
for (var i = 0; i < numbers.Length; i++) {
46-
numbers[i].next = numbers[(i + 1) % numbers.Length];
39+
var digits = input.Select(d => int.Parse(d.ToString())).ToArray();
40+
for (var i = 0; i < digits.Length - 1; i++) {
41+
next[digits[i]] = digits[i + 1];
4742
}
43+
next[digits.Last()] = digits.First();
4844

49-
this.currentCup = numbers[0];
50-
var cup = numbers[0];
51-
for (var i = 0; i < maxLabel; i++) {
52-
cupsByLabel[cup.label] = cup;
53-
cup = cup.next;
45+
if (maxLabel > input.Length) {
46+
(next[digits.Last()], next[maxLabel]) = (input.Length + 1, next[digits.Last()]);
5447
}
48+
49+
currentCup = digits.First();
5550
}
5651

5752
public IEnumerable<long> Labels() {
58-
var cup = cupsByLabel[1].next;
53+
var cup = next[1];
5954
while (true) {
60-
yield return cup.label;
61-
cup = cup.next;
55+
yield return cup;
56+
cup = next[cup];
6257
}
6358
}
6459

6560
public void Rotate() {
66-
var removed = currentCup.next;
67-
currentCup.next = currentCup.next.next.next.next;
68-
var destinationCup = currentCup.label;
69-
destinationCup = destinationCup == 1 ? maxLabel : destinationCup - 1;
70-
while (destinationCup == removed.label ||
71-
destinationCup == removed.next.label ||
72-
destinationCup == removed.next.next.label
61+
var removed = next[currentCup];
62+
next[currentCup] = next[next[next[removed]]];
63+
var destinationCup = currentCup == 1 ? next.Length - 1: currentCup - 1;
64+
65+
while (destinationCup == removed ||
66+
destinationCup == next[removed] ||
67+
destinationCup == next[next[removed]]
7368
) {
74-
destinationCup = destinationCup == 1 ? maxLabel : destinationCup - 1;
69+
destinationCup = destinationCup == 1 ? next.Length - 1 : destinationCup - 1;
7570
}
76-
var cup = cupsByLabel[destinationCup];
77-
removed.next.next.next = cup.next;
78-
cup.next = removed;
79-
currentCup = currentCup.next;
80-
}
81-
}
8271

83-
class Cup {
84-
public long label;
85-
public Cup next;
86-
public Cup(int item) {
87-
this.label = item;
72+
(next[destinationCup], next[next[next[removed]]]) = (removed, next[destinationCup]);
73+
currentCup = next[currentCup];
8874
}
8975
}
9076
}

0 commit comments

Comments
 (0)