-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2016_19.java
115 lines (98 loc) Β· 3.01 KB
/
AoC2016_19.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.List;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public class AoC2016_19 extends SolutionBase<String, Integer, Integer> {
private AoC2016_19(final boolean debug) {
super(debug);
}
public static AoC2016_19 create() {
return new AoC2016_19(false);
}
public static AoC2016_19 createDebug() {
return new AoC2016_19(true);
}
@Override
protected String parseInput(final List<String> inputs) {
return inputs.get(0);
}
@Override
public Integer solvePart1(final String input) {
final Elves elves = Elves.fromInput(input);
Node curr = elves.head;
while (elves.size > 1) {
final Node loser = curr.next;
elves.remove(loser);
curr = curr.next;
}
return curr.value;
}
@Override
public Integer solvePart2(final String input) {
final Elves elves = Elves.fromInput(input);
Node curr = elves.head;
Node opposite = elves.head;
for (int i = 0; i < elves.size / 2; i++) {
opposite = opposite.next;
}
while (elves.size > 1) {
final Node loser = opposite;
elves.remove(loser);
if (elves.size % 2 == 1) {
opposite = opposite.next;
} else {
opposite = opposite.next.next;
}
curr = curr.next;
}
return curr.value;
}
@Samples({
@Sample(method = "part1", input = TEST, expected = "3"),
@Sample(method = "part2", input = TEST, expected = "2"),
})
public static void main(final String[] args) throws Exception {
AoC2016_19.create().run();
}
private static final String TEST = "5";
private static final class Elves {
private Node head;
private int size;
public Elves() {
this.head = null;
this.size = 0;
}
public static Elves fromInput(final String input) {
final Elves elves = new Elves();
Node node = null;
Node prev = null;
for (int i = 0; i < Integer.parseInt(input); i++) {
node = new Node(i + 1);
if (elves.size == 0) {
elves.head = node;
} else {
node.prev = prev;
prev.next = node;
}
prev = node;
elves.size++;
}
elves.head.prev = node;
node.next = elves.head;
return elves;
}
public void remove(final Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
this.size--;
}
}
private static class Node {
public int value;
public Node next;
public Node prev;
public Node(final int value) {
this.value = value;
}
}
}