-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDay19.java
123 lines (104 loc) · 3.94 KB
/
Day19.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
116
117
118
119
120
121
122
123
package com.sbaars.adventofcode.year21.days;
import com.sbaars.adventofcode.common.Pair;
import com.sbaars.adventofcode.common.location.IntLoc;
import com.sbaars.adventofcode.common.location.Loc3D;
import com.sbaars.adventofcode.year21.Day2021;
import java.util.*;
import static com.sbaars.adventofcode.common.Pair.pair;
public class Day19 extends Day2021 {
public Day19() {
super(19);
}
public static void main(String[] args) {
new Day19().printParts();
}
@Override
public Object part1() {
return findScannerPositions().getLeft().locs.size();
}
private Scanner[] scannerVariants(Scanner sc) {
Scanner[] variations = new Scanner[24];
for (int up = 0; up < 6; up++) {
for (int rot = 0; rot < 4; rot++) {
variations[rot + up * 4] = new Scanner(sc, up, rot);
}
}
return variations;
}
protected Pair<Scanner, Loc3D[]> findScannerPositions() {
Scanner[][] scanners = Arrays.stream(day().trim().split("\n\n")).map(Scanner::new).map(this::scannerVariants).toArray(Scanner[][]::new);
var orientation = new Scanner[scanners.length];
var position = new Loc3D[scanners.length];
orientation[0] = scanners[0][0];
position[0] = new Loc3D(0, 0, 0);
Queue<Integer> queue = new ArrayDeque<>(List.of(0));
while (!queue.isEmpty()) {
var num = queue.poll();
for (int i = 0; i < scanners.length; i++) {
if (position[i] == null) {
var match = orientation[num].match(scanners[i]);
if (match.isPresent()) {
orientation[i] = match.get().getLeft();
Loc3D one = position[num];
Loc3D two = match.get().getRight();
position[i] = new Loc3D(one.x + two.x, one.y + two.y, one.z + two.z);
queue.add(i);
}
}
}
}
var result = new Scanner(new ArrayList<>(orientation[0].locs));
for (int i = 1; i < scanners.length; i++) {
result.add(orientation[i], position[i]);
}
return pair(result, position);
}
@Override
public Object part2() {
var p = findScannerPositions().getRight();
return IntLoc.range(p.length, p.length).mapToLong(l -> Math.abs(p[l.x].x - p[l.y].x) + Math.abs(p[l.x].y - p[l.y].y) + Math.abs(p[l.x].z - p[l.y].z)).max().getAsLong();
}
public record Scanner(List<Loc3D> locs) {
public Scanner(String s) {
this(Arrays.stream(s.substring(s.indexOf("\n") + 1).split("\n")).map(e -> Arrays.stream(e.split(",")).mapToLong(Long::parseLong).toArray()).map(Loc3D::new).toList());
}
public Scanner(Scanner other, int up, int rot) {
this(other.locs.stream().map(e -> e.flip(up).rotate(rot)).toList());
}
private Optional<Loc3D> findMatch(Scanner s) {
for (int i = 0; i < locs.size(); i++) {
for (int j = 0; j < s.locs.size(); j++) {
var a = locs.get(i);
var b = s.locs.get(j);
var relx = b.x - a.x;
var rely = b.y - a.y;
var relz = b.z - a.z;
int count = 0;
for (int k = 0; k < locs.size(); k++) {
if ((count + locs.size() - k) < 12) break;
for (int l = 0; l < s.locs.size(); l++) {
var m = locs.get(k);
var n = s.locs.get(l);
if ((relx + m.x) == n.x && (rely + m.y) == n.y && (relz + m.z) == n.z) {
count++;
if (count >= 12) return Optional.of(new Loc3D(relx, rely, relz));
break;
}
}
}
}
}
return Optional.empty();
}
public Optional<Pair<Scanner, Loc3D>> match(Scanner[] other) {
return Arrays.stream(other).map(e -> pair(e, findMatch(e))).filter(e -> e.getRight().isPresent()).map(e -> pair(e.getLeft(), e.getRight().get())).findFirst();
}
public void add(Scanner s, Loc3D p) {
for (int l = 0; l < s.locs.size(); l++) {
var n = s.locs.get(l);
n = new Loc3D(n.x - p.x, n.y - p.y, n.z - p.z);
if (!locs.contains(n)) locs.add(n);
}
}
}
}