-
Notifications
You must be signed in to change notification settings - Fork 17
/
Day23.java
56 lines (48 loc) · 1.65 KB
/
Day23.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
package com.sbaars.adventofcode.year20.days;
import com.sbaars.adventofcode.common.CircularLinkedList;
import com.sbaars.adventofcode.common.CircularLinkedList.Node;
import com.sbaars.adventofcode.year20.Day2020;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.lang.Long.parseLong;
import static java.util.Arrays.stream;
public class Day23 extends Day2020 {
public Day23() {
super(23);
}
public static void main(String[] args) {
new Day23().printParts();
}
@Override
public Object part1() {
return getSolution(true);
}
@Override
public Object part2() {
return getSolution(false);
}
private long getSolution(boolean part1) {
IntStream input = day().chars().map(Character::getNumericValue);
CircularLinkedList cups = new CircularLinkedList(Stream.concat(input.boxed(), part1 ? Stream.empty() : IntStream.rangeClosed(10, 1000000).boxed()).mapToInt(e -> e).toArray());
for (int i = 0; i < (part1 ? 100 : 10000000); i++) {
int current = cups.current();
int j;
Node next = cups.currentNode().next;
Node last = next.next.next;
for (j = current - 2 + cups.size(); j > 0; j--) {
int n = j % cups.size() + 1;
if (next.value != n && next.next.value != n && last.value != n) {
break;
}
}
int d = j % cups.size() + 1;
cups.insertAfter(next, last, d);
cups.next();
}
cups.setCurrent(1);
if (part1) return parseLong(stream(cups.next(8)).mapToObj(Integer::toString).collect(Collectors.joining()));
int[] next = cups.next(2);
return (long) next[0] * next[1];
}
}