-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC192016.java
61 lines (50 loc) · 1.54 KB
/
AoC192016.java
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
package com.adventofcode.aoc2016;
import static com.adventofcode.utils.Utils.atoi;
import static com.adventofcode.utils.Utils.getFirstString;
import static com.adventofcode.utils.Utils.itoa;
import com.adventofcode.Solution;
import com.adventofcode.utils.LinkedListNode;
import java.util.stream.Stream;
class AoC192016 implements Solution {
@Override
public String solveFirstPart(final Stream<String> input) {
return solve( input, true );
}
@Override
public String solveSecondPart(final Stream<String> input) {
return solve( input, false );
}
private String solve(final Stream<String> input, final boolean first) {
int size = atoi( getFirstString( input ) );
var curr = getCircularList( size );
curr = getNode( curr, first ? 0 : (size / 2) - 1 );
while ( size > 1 ) {
//remove node
curr.next = curr.next.next;
size--;
if ( first || size % 2 == 0 ) {
//move pointer
curr = curr.next;
}
}
return itoa( curr.value );
}
private LinkedListNode<Integer> getCircularList(final int size) {
final LinkedListNode<Integer> head = new LinkedListNode<>( null );
var curr = head;
for ( int i = 1; i <= size; i++ ) {
curr.next = new LinkedListNode<>( i );
curr = curr.next;
}
//circular list
curr.next = head.next;
return curr.next;
}
private <T> LinkedListNode<T> getNode(final LinkedListNode<T> head, final int index) {
var curr = head;
for ( int i = 0; i < index; i++ ) {
curr = curr.next;
}
return curr;
}
}