-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.java
More file actions
198 lines (182 loc) · 6.01 KB
/
Day14.java
File metadata and controls
198 lines (182 loc) · 6.01 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
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
189
190
191
192
193
194
195
196
197
198
import java.util.ArrayList;
import java.util.List;
public class Day14 {
public static void Run(List<String> input) {
p1(input);
p2(input);
}
private static CaveSlice ProcessInput(List<String> input, boolean withFloor)
{
var rockPaths= new ArrayList<ArrayList<Point>>(input.size());
int minX=500;
int maxX=500;
int maxY=0;
for(var line: input){
var pointStrings=line.split(" -> ");
var path = new ArrayList<Point>(pointStrings.length);
for (var pointString: pointStrings){
var xyStrings = pointString.split(",");
int x = Integer.parseInt(xyStrings[0]);
minX = Math.min(x, minX);
maxX = Math.max(x, maxX);
int y = Integer.parseInt(xyStrings[1]);
maxY = Math.max(y, maxY);
path.add(new Point(x,y));
}
rockPaths.add(path);
}
if (withFloor)
{
// floor will be at max + 2:
maxY += 2;
// make cave much wider than tall so we hit ceiling before sides:
minX-=(maxY*2);
maxX+=(maxY*2);
}
var slice = new CaveSlice(minX, maxX, maxY);
for(var path: rockPaths){
for (int i=0; i<path.size()-1; i++){
var point1=path.get(i);
var point2=path.get(i+1);
slice.addRockLine(point1.X, point1.Y, point2.X, point2.Y);
}
}
if (withFloor)
slice.addRockLine(minX, maxY, maxX, maxY);
return slice;
}
private static void p1(List<String> input){
var slice = ProcessInput(input, false);
int sandUnits=0;
DropResult result;
do{
result = slice.addSand(500);
if (result == DropResult.Rest)
sandUnits++;
} while (result == DropResult.Rest);
System.out.println("Day 14, p1: "+ sandUnits);
}
private static void p2(List<String> input){
var slice = ProcessInput(input, true);
int sandUnits=0;
DropResult result;
do{
result = slice.addSand(500);
if (result == DropResult.Rest)
sandUnits++;
else if (result==DropResult.Void){
System.out.println("Day 14 p2: make the cave wider!");
}
} while (result == DropResult.Rest);
System.out.println("Day 14, p2: "+ sandUnits);
}
private static enum CellContent {
Air,
Rock,
Sand,
Void
};
private static enum DropResult {
Rest, // came to rest somewhere
Void, // fell into the void
Blocked // starting position is not open
};
private static class CaveSlice{
private int _minX;
private CellContent[][] _contents;
public CaveSlice(int minX, int maxX,int maxY){
_minX=minX;
int width = (maxX-minX+1);
_contents = new CellContent[maxY+1][width];
for (int row=0; row <= maxY; row++)
for (int col=0; col < width; col++ )
_contents[row][col]=CellContent.Air;
}
public void addRockLine(int x1, int y1, int x2, int y2)
{
if (x1 == x2)
{
// vertical line
int start = Math.min(y1, y2);
int end = Math.max(y1, y2);
x1 -= _minX;
for (int y=start; y<=end; y++)
_contents[y][x1] = CellContent.Rock;
}
else
{
// horizontal line
x1 -= _minX;
x2 -= _minX;
int start = Math.min(x1, x2);
int end = Math.max(x1, x2);
for (int x=start; x <= end; x++)
_contents[y1][x] = CellContent.Rock;
}
}
public DropResult addSand(int startX){
var content = getContent(startX, 0);
if (content == CellContent.Void)
return DropResult.Void;
else if (content != CellContent.Air)
return DropResult.Blocked;
int x=startX;
int y=0; // always starts at y=0
while(true)
{
// straight down?
content = getContent(x,y+1);
if (content==CellContent.Void)
return DropResult.Void;
else if (content==CellContent.Air) {
y++;
continue;
}
// down and left?
content = getContent(x-1, y+1);
if (content==CellContent.Void)
return DropResult.Void;
else if (content==CellContent.Air) {
x--;
y++;
continue;
}
// down and right?
content = getContent(x+1, y+1);
if (content==CellContent.Void)
return DropResult.Void;
else if (content==CellContent.Air) {
x++;
y++;
continue;
}
break;
}
setContent(x,y, CellContent.Sand);
return DropResult.Rest;
}
public CellContent getContent(int x, int y)
{
int col = x-_minX;
if ((col < 0) || (col > _contents[0].length) || (y < 0) || (y >= _contents.length)){
return CellContent.Void;
}
return _contents[y][col];
}
private void setContent(int x, int y, CellContent newContent)
{
int col = x-_minX;
if (getContent(x, y) != CellContent.Void)
_contents[y][x - _minX] = newContent;
}
}
private static class Point{
public int X;
public int Y;
public Point(int x, int y)
{
X=x;
Y=y;
}
}
}