Repository navigation
Expand file tree
/
Copy pathSpinlock_17.java
More file actions
90 lines (78 loc) · 2.23 KB
/
Copy pathSpinlock_17.java
File metadata and controls
90 lines (78 loc) · 2.23 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
package adventofcode;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Why Did you create this class? what does it do?
*/
@SuppressWarnings("Duplicates") public class Spinlock_17 {
public static void main(String[] args) {
System.out.println(solve2(301));
}
private static int solve(int n) {
if (n <= 0)
return 0;
Map<Node, Integer> map = new HashMap<>();
Node current = new Node(0, null);
Node root = current;
map.put(current, 0);
Node ipNode;
for (int i = 1; i <= 2017; i++) {
int ip = (n + map.get(current)) % map.size();
ipNode = findByIndex(root, ip);
ipNode.next = new Node(i, ipNode.next);
map.put(ipNode.next, ip + 1);
current = ipNode.next;
}
return current.next.value;
}
private static int solve2(int n) {
if (n <= 0)
return 0;
int k = 0;
int currentIndex = 0;
for (int i = 1; i <= 50_000_000; i++) {
currentIndex = (n + currentIndex) % i + 1;
if (currentIndex - 1 == 0) {
k = i;
}
}
return k;
}
private static void printNode(Node root) {
while (root != null) {
System.out.print(root.value + " ");
root = root.next;
}
System.out.println();
}
private static Node findByIndex(Node root, int ip) {
int i = 0;
while (i < ip) {
root = root.next;
i++;
}
return root;
}
private static class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Node node = (Node) o;
if (value != node.value)
return false;
return next != null ? next.equals(node.next) : node.next == null;
}
@Override public int hashCode() {
return value;
}
}
}