-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.java
189 lines (162 loc) · 6.61 KB
/
Day15.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package aoc2019.day15;
import aoc2019.day09.Output;
import org.apache.commons.lang3.ArrayUtils;
import java.awt.*;
import java.util.List;
import java.util.*;
import static aoc2019.day09.Day9Kt.processParameterMode;
public class Day15 {
static Map<Point, Cell> processInput(ArrayList<Long> numbers) {
int index = 0;
int relativeBase = 0;
Map<Point, Cell> map = new HashMap<>();
Point currentPoint = new Point(0, 0);
map.put(currentPoint, new Cell(CellType.PATH));
StringBuilder outputBuilder = new StringBuilder();
int[] dirs = new int[]{3, 1, 4, 2}; //west, north, east, south
long k = 110000;
Direction direction = Direction.WEST;
loop:
while (k-- > 0) {
int opCode = Math.toIntExact(numbers.get(index));
Output output = processParameterMode(numbers, index, opCode, direction.getDirectionId(), relativeBase);
outputBuilder.append(output.getCode());
if (output.getRelativeBase() != 0) {
relativeBase = output.getRelativeBase();
}
if (outputBuilder.length() == 1) {
Point nextPoint = getNextCellCoordinates(currentPoint, direction);
switch (outputBuilder.toString()) {
case "0":
int rotatingId = (ArrayUtils.indexOf(dirs, direction.getDirectionId()) + 1) % 4; // index of current direction id in dirs + 1
direction = Direction.getValueOf(dirs[rotatingId]);
if (map.get(nextPoint) == null) {
map.put(nextPoint, new Cell(CellType.WALL));
}
break;
case "2":
case "1":
direction = getRelativeWestOfCurrentDirection(direction);
if (map.get(nextPoint) == null) {
map.put(nextPoint, new Cell(outputBuilder.toString().equals("1") ? CellType.PATH : CellType.OXIGEN_THING));
}
currentPoint = new Point(nextPoint.x, nextPoint.y);
break;
default:
System.out.println("Unexpected output");
break loop;
}
outputBuilder = new StringBuilder();
}
index += output.getIndex();
}
printCellMap(map);
return map;
}
static int findStepsToOxigenMachine(Map<Point, Cell> maze) {
Point currentPoint = new Point(0, 0);
Set<Point> visited = new HashSet<>();
Point fork = null;
int count = 0;
int countAtFork = -1;
while (!maze.get(currentPoint).getType().equals(CellType.OXIGEN_THING)) {
visited.add(currentPoint);
List<Point> adjacentPoints = getAdjacentPoints(currentPoint);
List<Point> possiblePaths = new ArrayList<>();
for (Point ap : adjacentPoints) {
if (!maze.get(ap).getType().equals(CellType.WALL) && !visited.contains(ap)) {
possiblePaths.add(ap);
}
}
switch (possiblePaths.size()) {
case 1:
count++;
currentPoint = possiblePaths.get(0);
break;
case 2:
count++;
fork = currentPoint;
countAtFork = count;
currentPoint = possiblePaths.get(0);
break;
case 0:
currentPoint = fork;
count = countAtFork;
}
}
return count - 1;
}
static int floodWithOxigen(Map<Point, Cell> maze) {
Point oxigenMachine = new Point(-16, 14);
Set<Point> edge = new HashSet<>();
Set<Point> visited = new HashSet<>();
edge.add(oxigenMachine);
List<Point> possiblePaths = new ArrayList<>();
int count = 0;
do {
count++;
List<Point> adjacentPoints = getAdjacentPoints(oxigenMachine);
for (Point ap : adjacentPoints) {
if (!maze.get(ap).getType().equals(CellType.WALL) && !edge.contains(ap)) {
possiblePaths.add(ap);
}
}
edge = new HashSet<>();
} while (edge.size() != maze.size());
return count;
}
static List<Point> getAdjacentPoints(Point p) {
List<Point> adjacentPoints = new ArrayList<>();
adjacentPoints.add(new Point(p.x - 1, p.y));
adjacentPoints.add(new Point(p.x, p.y + 1));
adjacentPoints.add(new Point(p.x + 1, p.y));
adjacentPoints.add(new Point(p.x, p.y - 1));
return adjacentPoints;
}
static Direction getRelativeWestOfCurrentDirection(Direction currentDirection) {
switch (currentDirection) {
case NORTH:
return Direction.WEST;
case EAST:
return Direction.NORTH;
case SOUTH:
return Direction.EAST;
default:
return Direction.SOUTH;
}
}
private static Point getNextCellCoordinates(Point currentCell, Direction direction) {
switch (direction) {
case NORTH:
return new Point(currentCell.x, currentCell.y + 1);
case EAST:
return new Point(currentCell.x + 1, currentCell.y);
case SOUTH:
return new Point(currentCell.x, currentCell.y - 1);
case WEST:
return new Point(currentCell.x - 1, currentCell.y);
}
return null;
}
static void printCellMap(Map<Point, Cell> cellMap) {
int minX = cellMap.keySet().stream().mapToInt(p -> p.x).min().orElse(-1);
int maxX = cellMap.keySet().stream().mapToInt(p -> p.x).max().orElse(-1);
int minY = cellMap.keySet().stream().mapToInt(p -> p.y).min().orElse(-1);
int maxY = cellMap.keySet().stream().mapToInt(p -> p.y).max().orElse(-1);
for (int y = maxY; y >= minY; y--) {
for (int x = minX; x <= maxX; x++) {
Point key = new Point(x, y);
if (cellMap.containsKey(key)) {
if (new Point(0, 0).equals(key)) {
System.out.print(0);
} else {
System.out.print(cellMap.get(key).getType().getSymbol());
}
} else {
System.out.print("░");
}
}
System.out.println();
}
}
}