-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay14.java
117 lines (92 loc) · 3.04 KB
/
Day14.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
package aoc17;
import java.util.HashSet;
import java.util.Set;
import myutils17.Point2d;
public class Day14 {
private final String INPUT;
public Day14(String input) {
this.INPUT = input;
}
// part 1
public int usedSquares() {
char[][] grid = diskGrid();
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
if (grid[i][j] == '1')
count++;
}
}
return count;
}
// part 2
public int regions() {
char[][] grid = diskGrid();
int count = 0;
Set<Point2d> marked = new HashSet<>();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
Point2d currentPoint = new Point2d(j, i);
if (!marked.contains(currentPoint) && grid[i][j] == '1') {
floodFill(currentPoint, marked, grid);
count++;
}
}
}
return count;
}
private void floodFill(Point2d currentPoint, Set<Point2d> marked, char[][] grid) {
if (marked.contains(currentPoint))
return;
marked.add(currentPoint);
Point2d up = new Point2d(currentPoint.x(), currentPoint.y() - 1);
Point2d down = new Point2d(currentPoint.x(), currentPoint.y() + 1);
Point2d left = new Point2d(currentPoint.x() - 1, currentPoint.y());
Point2d right = new Point2d(currentPoint.x() + 1, currentPoint.y());
if (up.y() >= 0 && !marked.contains(up) && grid[up.y()][up.x()] == '1')
floodFill(up, marked, grid);
if (down.y() < grid.length && !marked.contains(down) && grid[down.y()][down.x()] == '1')
floodFill(down, marked, grid);
if (left.x() >= 0 && !marked.contains(left) && grid[left.y()][left.x()] == '1')
floodFill(left, marked, grid);
if (right.x() < grid.length && !marked.contains(right) && grid[right.y()][right.x()] == '1')
floodFill(right, marked, grid);
}
private char[][] diskGrid() {
int maxRows = 128;
char[][] grid = new char[maxRows][];
for (int i = 0; i < maxRows; i++) {
int column = 0;
String toHash = INPUT + "-" + i;
Day10 knotHashCalculator = new Day10(toHash);
String hashedInputHex = knotHashCalculator.knotHash();
String hashedInputBin = knotHashToBinString(hashedInputHex);
grid[i] = new char[maxRows];
for (int j = 0; j < hashedInputBin.length(); j++) {
grid[i][column++] = hashedInputBin.charAt(j);
}
}
return grid;
}
private String knotHashToBinString(String knotHash) {
StringBuilder knotHashBinary = new StringBuilder();
for (int i = 0; i < knotHash.length(); i++) {
int hexValue = Integer.parseInt(Character.toString(knotHash.charAt(i)), 16);
String binValue = Integer.toBinaryString(hexValue);
if (binValue.length() == 1)
binValue = "000" + binValue;
else if (binValue.length() == 2)
binValue = "00" + binValue;
else if (binValue.length() == 3)
binValue = "0" + binValue;
knotHashBinary.append(binValue);
}
return knotHashBinary.toString();
}
public static void main(String[] args) {
String input = "ffayrhll";
// String exampleInput = "flqrgnkx";
Day14 test = new Day14(input);
System.out.println(test.regions());
}
}