-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14a.java
More file actions
141 lines (126 loc) · 3.5 KB
/
Day14a.java
File metadata and controls
141 lines (126 loc) · 3.5 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
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
public class Day14a {
public static String myRead(Scanner scanner) {
String line;
try {
line = scanner.nextLine().trim();
} catch (Exception e) {
line = null;
}
return line;
}
public static Scanner myScanner(File text) {
Scanner scanner;
try {
scanner = new Scanner(text);
} catch (Exception e) {
System.out.println("No Scanner");
return null;
}
return scanner;
}
public static void main(String[] args) {
File text = new File("/Users/axelteo/Desktop/AdventofCode/Day14input.txt");
Scanner scanner = myScanner(text);
if (scanner == null) {
return;
}
int[][] occupied = new int[200][600];
int yLimit = 0;
String line;
while ((line = myRead(scanner)) != null) {
String[] coordList = line.split(" -> ", 0);
Coord curr, next;
String[] xyCoord;
xyCoord = coordList[0].split(",", 0);
if (Integer.valueOf(xyCoord[1]) > yLimit) {
yLimit = Integer.valueOf(xyCoord[1]);
}
curr = new Coord(Integer.valueOf(xyCoord[0]), Integer.valueOf(xyCoord[1]));
curr.addTo(occupied);
for (int i = 1; i < coordList.length; ++i) {
xyCoord = coordList[i].split(",", 0);
if (Integer.valueOf(xyCoord[1]) > yLimit) {
yLimit = Integer.valueOf(xyCoord[1]);
}
next = new Coord(Integer.valueOf(xyCoord[0]), Integer.valueOf(xyCoord[1]));
ArrayList<Coord> tmp = curr.rocksFromTo(next);
for (Coord x: tmp) {
x.addTo(occupied);
}
curr = next;
}
}
Coord newSand;
Coord spawn = new Coord(500, 0);
int count = 0;
while ((newSand = Coord.restsAt(spawn, occupied, yLimit)) != null) {
++count;
newSand.addTo(occupied);
}
System.out.println("units of sand that rest: " + count);
}
}
class Coord {
int x;
int y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
public void addTo(int[][] occupied) {
occupied[this.y][this.x] = 1;
}
// returns arrayList of Coords between this and next, excluding this.
public ArrayList<Coord> rocksFromTo(Coord next) {
ArrayList<Coord> res = new ArrayList<>();
int xDiff = next.x - this.x;
int yDiff = next.y - this.y;
for (int i = 0; i < xDiff; ++i) {
res.add(new Coord(this.x + i + 1, this.y));
}
for (int i = 0; i > xDiff; --i) {
res.add(new Coord(this.x + i - 1, this.y));
}
for (int i = 0; i < yDiff; ++i) {
res.add(new Coord(this.x, this.y + i + 1));
}
for (int i = 0; i > yDiff; --i) {
res.add(new Coord(this.x, this.y + i - 1));
}
return res;
}
public static Coord restsAt(Coord start, int[][] grid, int yLimit) {
if (start.y > yLimit) {
return null;
}
if (grid[start.y+1][start.x] == 0) {
return Coord.restsAt(new Coord(start.x, start.y + 1), grid, yLimit);
}
else if (grid[start.y+1][start.x-1] == 0) {
return Coord.restsAt(new Coord(start.x-1, start.y + 1), grid, yLimit);
}
else if (grid[start.y+1][start.x+1] == 0) {
return Coord.restsAt(new Coord(start.x+1, start.y + 1), grid, yLimit);
} else {
return start;
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Coord)) {
return false;
}
Coord some = (Coord) obj;
if (this.x == some.x && this.y == some.y) {
return true;
}
return false;
}
@Override
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
}