@@ -29,62 +29,48 @@ public object PartTwo(string input) {
29
29
}
30
30
31
31
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 ;
35
34
36
35
public Cups ( string input , int maxLabel ) {
37
- this . maxLabel = maxLabel ;
36
+ next = Enumerable . Range ( 1 , maxLabel + 1 ) . ToArray ( ) ;
37
+ next [ 0 ] = - 1 ;
38
38
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 ] ;
47
42
}
43
+ next [ digits . Last ( ) ] = digits . First ( ) ;
48
44
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 ( ) ] ) ;
54
47
}
48
+
49
+ currentCup = digits . First ( ) ;
55
50
}
56
51
57
52
public IEnumerable < long > Labels ( ) {
58
- var cup = cupsByLabel [ 1 ] . next ;
53
+ var cup = next [ 1 ] ;
59
54
while ( true ) {
60
- yield return cup . label ;
61
- cup = cup . next ;
55
+ yield return cup ;
56
+ cup = next [ cup ] ;
62
57
}
63
58
}
64
59
65
60
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 ] ]
73
68
) {
74
- destinationCup = destinationCup == 1 ? maxLabel : destinationCup - 1 ;
69
+ destinationCup = destinationCup == 1 ? next . Length - 1 : destinationCup - 1 ;
75
70
}
76
- var cup = cupsByLabel [ destinationCup ] ;
77
- removed . next . next . next = cup . next ;
78
- cup . next = removed ;
79
- currentCup = currentCup . next ;
80
- }
81
- }
82
71
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 ] ;
88
74
}
89
75
}
90
76
}
0 commit comments