-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.java
More file actions
117 lines (97 loc) · 3.65 KB
/
Day03.java
File metadata and controls
117 lines (97 loc) · 3.65 KB
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
package se.augustocesar.aocjava.y2019;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import se.augustocesar.aocjava.Day;
import se.augustocesar.aocjava.RunnableDay;
import se.augustocesar.aocjava.utils.Pair;
import se.augustocesar.aocjava.utils.Point2D;
@RunnableDay(year = 2019, day = 3)
public class Day03 extends Day {
@Override
public String partOne() {
final List<HashMap<String, Pair<Point2D, Integer>>> wires = getWiresPaths();
int shortest = Integer.MAX_VALUE;
List<Pair<Point2D, Integer>> intersect = intersect(wires.get(0), wires.get(1));
for (Pair<Point2D, Integer> pointSteps : intersect) {
final int distance = pointSteps.getLeft().taxicab(Point2D.on(0, 0));
if (distance < shortest) {
shortest = distance;
}
}
return String.valueOf(shortest);
}
@Override
public String partTwo() {
final List<HashMap<String, Pair<Point2D, Integer>>> wires = getWiresPaths();
int shortest = Integer.MAX_VALUE;
List<Pair<Point2D, Integer>> intersect = intersect(wires.get(0), wires.get(1));
for (Pair<Point2D, Integer> pointSteps : intersect) {
final int distance = pointSteps.getRight();
if (distance < shortest) {
shortest = distance;
}
}
return String.valueOf(shortest);
}
private List<HashMap<String, Pair<Point2D, Integer>>> getWiresPaths() {
List<String> input = this.readInputLines();
List<HashMap<String, Pair<Point2D, Integer>>> wires = new ArrayList<>();
for (String in : input) {
wires.add(generatePath(Point2D.on(0, 0), in));
}
return wires;
}
private HashMap<String, Pair<Point2D, Integer>> generatePath(
final Point2D basePoint, final String pathCommands) {
List<String> commands = List.of(pathCommands.split(","));
HashMap<String, Pair<Point2D, Integer>> path = new HashMap<>(commands.size());
int steps = 0;
Point2D lastPoint = basePoint;
for (String command : commands) {
char direction = command.charAt(0);
int amount = Integer.parseInt(command.substring(1));
for (int i = 0; i < amount; i++) {
steps++;
switch (direction) {
case 'U' -> lastPoint = lastPoint.moveY(1);
case 'R' -> lastPoint = lastPoint.moveX(1);
case 'D' -> lastPoint = lastPoint.moveY(-1);
case 'L' -> lastPoint = lastPoint.moveX(-1);
default -> {
continue;
}
}
path.put(lastPoint.id(), Pair.of(lastPoint, steps));
}
}
return path;
}
/**
* Generates the intersection of two wire paths.
*
* <p>The input paths consists of: {@link HashMap} of {@link String} (Key of the {@link Point2D})
* to a {@link Pair} of {@link Point2D} and {@link Integer} (steps taken to reach the {@link
* Point2D}).
*
* @param pathOne Path of the first wire.
* @param pathTwo Path of the second wire.
* @return The intersection of both paths paired with the summed steps to get to the {@link
* Point2D} by both wires.
*/
private List<Pair<Point2D, Integer>> intersect(
final HashMap<String, Pair<Point2D, Integer>> pathOne,
final HashMap<String, Pair<Point2D, Integer>> pathTwo) {
List<Pair<Point2D, Integer>> intersection = new ArrayList<>();
for (Map.Entry<String, Pair<Point2D, Integer>> entry : pathOne.entrySet()) {
Pair<Point2D, Integer> pathTwoEntry = pathTwo.get(entry.getValue().getLeft().id());
if (pathTwoEntry != null) {
intersection.add(
Pair.of(
entry.getValue().getLeft(), entry.getValue().getRight() + pathTwoEntry.getRight()));
}
}
return intersection;
}
}