1
+ package com .macasaet ;
2
+
3
+ import java .io .IOException ;
4
+ import java .math .BigInteger ;
5
+ import java .util .ArrayList ;
6
+
7
+ public class Day23 {
8
+
9
+ public static void main (final String [] args ) throws IOException {
10
+ final var labels = new ArrayList <Integer >();
11
+ try (var inputStream = Day23 .class .getResourceAsStream ("/day-23-input.txt" )) {
12
+ for (final var c : new String (inputStream .readAllBytes ()).strip ().toCharArray ()) {
13
+ labels .add (Integer .parseInt ("" + c ));
14
+ }
15
+ }
16
+
17
+ final var map = new Cup [1_000_0000 + 1 ];
18
+ Cup first = null ;
19
+ Cup current = null ;
20
+ int min = Integer .MAX_VALUE ;
21
+ int max = Integer .MIN_VALUE ;
22
+ for (final int label : labels ) {
23
+ final var cup = new Cup (label );
24
+ map [label ] = cup ;
25
+ if (first == null ) {
26
+ first = cup ;
27
+ } else {
28
+ current .next = cup ;
29
+ }
30
+ current = cup ;
31
+
32
+ min = Math .min (min , label );
33
+ max = Math .max (max , label );
34
+ }
35
+
36
+ for (int i = max + 1 ; i <= 1_000_000 ; i ++) { // Part 2
37
+ final var cup = new Cup (i );
38
+ map [i ] = cup ;
39
+ current .next = cup ;
40
+ current = cup ;
41
+ }
42
+ max = 1_000_000 ;
43
+ current .next = first ;
44
+ current = current .next ;
45
+
46
+ for (int moveId = 1 ; moveId <= 10_000_000 ; moveId ++) { // Part 1 looped only 100 times
47
+ final var a = current .take ();
48
+ final var b = current .take ();
49
+ final var c = current .take ();
50
+
51
+ int destinationValue = current .label - 1 ;
52
+ while (destinationValue == a .label || destinationValue == b .label || destinationValue == c .label || destinationValue < min || destinationValue > max ) {
53
+ destinationValue --;
54
+ if (destinationValue < min ) {
55
+ destinationValue = max ;
56
+ }
57
+ }
58
+
59
+ final var destination = map [destinationValue ];
60
+ c .linkBefore (destination .next );
61
+ b .linkBefore (c );
62
+ a .linkBefore (b );
63
+ destination .linkBefore (a );
64
+
65
+ current = current .next ;
66
+ }
67
+ final var reference = map [1 ];
68
+ // String result = "";
69
+ // var cursor = reference.next;
70
+ // for (int i = labels.size() - 1; --i >= 0; ) {
71
+ // result += cursor.label;
72
+ // cursor = cursor.next;
73
+ // }
74
+ // System.out.println("Part 1: " + result);
75
+ final BigInteger x = new BigInteger ("" + map [reference .next .label ].label );
76
+ final BigInteger y = new BigInteger ("" + map [reference .next .next .label ].label );
77
+ System .out .println ("Part 2: " + x .multiply (y ).toString ());
78
+ }
79
+
80
+ protected static class Cup {
81
+ private final int label ;
82
+ private Cup next ;
83
+
84
+ public Cup (final int label ) {
85
+ this .label = label ;
86
+ }
87
+
88
+ public Cup take () {
89
+ final Cup retval = next ;
90
+ next = retval .next ;
91
+ retval .next = null ;
92
+ return retval ;
93
+ }
94
+
95
+ public void linkBefore (final Cup cup ) {
96
+ next = cup ;
97
+ }
98
+
99
+ public String toString () {
100
+ return "Cup{ label=" + label + ", next=" + (next != null ? next .label : null ) + " }" ;
101
+ }
102
+ }
103
+
104
+ }
0 commit comments