-
Notifications
You must be signed in to change notification settings - Fork 5
/
Day3.java
92 lines (81 loc) · 3 KB
/
Day3.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
package aoc18;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.awt.geom.Point2D;
public class Day3 {
private static int getId(String claim) {
return Integer.parseInt(claim.substring(1, claim.indexOf("@") - 1));
}
private static int[] getCoordinates(String claim) {
int x = Integer.parseInt(claim.substring(claim.indexOf("@") + 2, claim.indexOf(",")));
int y = Integer.parseInt(claim.substring(claim.indexOf(",") + 1, claim.indexOf(":")));
return new int[] { x, y };
}
private static int[] getDimensions(String claim) {
int width = Integer.parseInt(claim.substring(claim.indexOf(":") + 2, claim.indexOf("x")));
int height = Integer.parseInt(claim.substring(claim.indexOf("x") + 1, claim.length()));
return new int[] { width, height };
}
// part 1
public static Set<Point2D.Double> overlappingArea(List<String> fabricClaims) {
Set<Point2D.Double> allCoordinates = new HashSet<Point2D.Double>();
Set<Point2D.Double> overlappingCoordinates = new HashSet<Point2D.Double>();
for (String claim : fabricClaims) {
int[] coordinates = getCoordinates(claim);
int[] dimensions = getDimensions(claim);
// starting coordinates for the current rectangle
int x = coordinates[0];
int y = coordinates[1];
// dimensions of the current rectangle
// rectangle
int width = dimensions[0];
int height = dimensions[1];
for (int i = y; i < y + height; i++) {
for (int j = x; j < x + width; j++) {
Point2D.Double currentPoint = new Point2D.Double(j, i);
if (allCoordinates.contains(currentPoint)) {
overlappingCoordinates.add(currentPoint);
}
allCoordinates.add(currentPoint);
}
}
}
return overlappingCoordinates;
}
// part 2
// expects the solution of part 1 as a parameter
public static int getAreaIdWithoutOverlap(Set<Point2D.Double> overlapCoordinates, List<String> fabricClaims) {
for (String claim : fabricClaims) {
int currentId = getId(claim);
int[] coordinates = getCoordinates(claim);
int[] dimensions = getDimensions(claim);
int x = coordinates[0];
int y = coordinates[1];
int width = dimensions[0];
int height = dimensions[1];
boolean hasOverlap = false;
for (int i = y; i < y + height; i++) {
for (int j = x; j < x + width; j++) {
// if the current coordinate is inside the overlap set it
// can't be the target
if (overlapCoordinates.contains(new Point2D.Double(j, i))) {
hasOverlap = true;
}
}
}
if (!hasOverlap)
return currentId;
}
// no overlaps or wrong input
return -1;
}
public static void main(String[] args) throws IOException {
List<String> fabricClaims = aoc18.Day1
.inputFileToList(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2018\\Day 3\\InputFile.txt"));
Set<Point2D.Double> overlappingCoordinates = overlappingArea(fabricClaims);
System.out.println(getAreaIdWithoutOverlap(overlappingCoordinates, fabricClaims));
}
}