-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_10.java
More file actions
119 lines (88 loc) · 3.58 KB
/
Day_10.java
File metadata and controls
119 lines (88 loc) · 3.58 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
118
119
import java.awt.*;
import java.util.HashMap;
import java.util.List;
public class Day_10 extends AocSolver {
protected Day_10(String filename) {
super(filename);
}
public static void main(String[] args) {
new Day_10("resources/day_10.txt");
}
@Override
protected String runPart1(List<String> lines) {
final HashMap<Point, Character> pipesMap = new HashMap<>();
Point start = new Point();
for (int y = 0; y < lines.size(); y++) {
char[] characters = lines.get(y).toCharArray();
for (int x = 0; x < characters.length; x++) {
char character = characters[x];
Point point = new Point(x, -y);
pipesMap.put(point, character);
if (character == 'S') {
start.setLocation(point);
}
}
}
int moves = 1;
// PositionTracker tracker1 = new PositionTracker(1, -1, start, pipesMap);
// PositionTracker tracker2 = new PositionTracker(3, -1, start, pipesMap);
PositionTracker tracker1 = new PositionTracker(42, -92, start, pipesMap);
PositionTracker tracker2 = new PositionTracker(44, -92, start, pipesMap);
while (!tracker1.getCurrentPosition().equals(tracker2.getCurrentPosition())) {
moves++;
tracker1.moveToNext();
tracker2.moveToNext();
}
return Integer.toString(moves);
}
@Override
protected String runPart2(List<String> lines) {
// expand everything to 3x3
// flood fill
// count only the original tiles that are flooded
// (how to expand S?)
return "hi mom";
}
}
class PositionTracker {
private final Point currentPosition;
private final Point previousPosition;
final private HashMap<Point, Character> pipesMap;
public PositionTracker(int x, int y, Point previousPosition, HashMap<Point, Character> pipesMap) {
currentPosition = new Point(x, y);
this.previousPosition = previousPosition.getLocation();
this.pipesMap = pipesMap;
}
public Point getCurrentPosition() {
return currentPosition;
}
public Point moveToNext() {
char currentPipe = pipesMap.get(currentPosition);
int startOffsetX = previousPosition.x - currentPosition.x;
int startOffsetY = previousPosition.y - currentPosition.y;
Point startOffset = new Point(startOffsetX, startOffsetY);
final HashMap<Character, Pair> pipeConversions = new HashMap<>();
pipeConversions.put('|', new Pair(new Point(0, -1), new Point(0, 1)));
pipeConversions.put('-', new Pair(new Point(-1, 0), new Point(1, 0)));
pipeConversions.put('L', new Pair(new Point(0, 1), new Point(1, 0)));
pipeConversions.put('J', new Pair(new Point(-1, 0), new Point(0, 1)));
pipeConversions.put('7', new Pair(new Point(-1, 0), new Point(0, -1)));
pipeConversions.put('F', new Pair(new Point(0, -1), new Point(1, 0)));
Pair pipeConversion = pipeConversions.get(currentPipe);
Point endOffset = pipeConversion.getOther(startOffset);
previousPosition.setLocation(currentPosition);
currentPosition.setLocation(currentPosition.x + endOffset.x, currentPosition.y + endOffset.y);
return currentPosition;
}
}
class Pair {
final private Point val1;
final private Point val2;
public Pair(Point val1, Point val2) {
this.val1 = val1;
this.val2 = val2;
}
public Point getOther(Point val) {
return val.equals(val1) ? val2 : val1;
}
}