-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay14.java
141 lines (117 loc) · 4.1 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.sbaars.adventofcode.year17.days;
import com.sbaars.adventofcode.common.Day;
public class Day14 extends Day {
private static final int GRID_SIZE = 128;
public Day14() {
super(14, 2017);
}
public static void main(String[] args) {
new Day14().printParts();
}
private boolean[][] createGrid(String input) {
boolean[][] grid = new boolean[GRID_SIZE][GRID_SIZE];
for (int row = 0; row < GRID_SIZE; row++) {
String rowInput = input + "-" + row;
String hash = knotHash(rowInput);
// Convert hash to binary and fill grid
int col = 0;
for (char c : hash.toCharArray()) {
int value = Character.digit(c, 16);
for (int bit = 3; bit >= 0; bit--) {
grid[row][col] = ((value >> bit) & 1) == 1;
col++;
}
}
}
return grid;
}
private void dfs(boolean[][] grid, boolean[][] visited, int row, int col) {
if (row < 0 || row >= GRID_SIZE || col < 0 || col >= GRID_SIZE ||
!grid[row][col] || visited[row][col]) {
return;
}
visited[row][col] = true;
// Visit all adjacent squares
dfs(grid, visited, row - 1, col); // up
dfs(grid, visited, row + 1, col); // down
dfs(grid, visited, row, col - 1); // left
dfs(grid, visited, row, col + 1); // right
}
@Override
public Object part1() {
String input = "hxtvlmkl";
int usedSquares = 0;
for (int i = 0; i < GRID_SIZE; i++) {
String rowInput = input + "-" + i;
String hash = knotHash(rowInput);
usedSquares += countBits(hash);
}
return usedSquares;
}
private String knotHash(String input) {
int[] list = new int[256];
for (int i = 0; i < 256; i++) {
list[i] = i;
}
int[] lengths = new int[input.length() + 5];
for (int i = 0; i < input.length(); i++) {
lengths[i] = input.charAt(i);
}
lengths[input.length()] = 17;
lengths[input.length() + 1] = 31;
lengths[input.length() + 2] = 73;
lengths[input.length() + 3] = 47;
lengths[input.length() + 4] = 23;
int pos = 0;
int skip = 0;
for (int round = 0; round < 64; round++) {
for (int length : lengths) {
// Reverse the sublist
for (int i = 0; i < length / 2; i++) {
int a = (pos + i) % 256;
int b = (pos + length - 1 - i) % 256;
int temp = list[a];
list[a] = list[b];
list[b] = temp;
}
pos = (pos + length + skip) % 256;
skip++;
}
}
// Calculate dense hash
StringBuilder result = new StringBuilder();
for (int i = 0; i < 16; i++) {
int xor = 0;
for (int j = 0; j < 16; j++) {
xor ^= list[i * 16 + j];
}
result.append(String.format("%02x", xor));
}
return result.toString();
}
private int countBits(String hash) {
int count = 0;
for (char c : hash.toCharArray()) {
int value = Character.digit(c, 16);
count += Integer.bitCount(value);
}
return count;
}
@Override
public Object part2() {
String input = "hxtvlmkl";
boolean[][] grid = createGrid(input);
boolean[][] visited = new boolean[GRID_SIZE][GRID_SIZE];
int regions = 0;
// Count regions using DFS
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
if (grid[row][col] && !visited[row][col]) {
dfs(grid, visited, row, col);
regions++;
}
}
}
return regions;
}
}