Skip to content

Commit 3dfc878

Browse files
authored
Create Day14.java
1 parent 299e3e7 commit 3dfc878

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

Day14.java

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.adventofcode.advent2017;
2+
3+
import java.util.Arrays;
4+
import java.util.Queue;
5+
import java.util.concurrent.ArrayBlockingQueue;
6+
7+
public class Day14 {
8+
static int part1(String input) {
9+
int[] stringOrig = new int[256];
10+
for (int i = 0; i < 256; i++) {
11+
stringOrig[i] = i;
12+
}
13+
int[] string = Arrays.copyOf(stringOrig, 256);
14+
15+
byte[][] grid = new byte[128][128];
16+
17+
int count = 0;
18+
for (int i = 0; i < 128; i++) {
19+
count += part1((input + '-' + i).getBytes(), string, grid[i]);
20+
System.arraycopy(stringOrig, 0, string, 0, 256);
21+
}
22+
23+
// return count; // Part 1
24+
25+
return part2(grid);
26+
}
27+
28+
29+
static class Point {
30+
int x;
31+
32+
int y;
33+
34+
public Point(int x, int y) {
35+
this.x = x;
36+
this.y = y;
37+
}
38+
}
39+
40+
static int part2(byte[][] grid) {
41+
Queue<Point> pointQueue = new ArrayBlockingQueue<>(1000);
42+
43+
int regions = 0;
44+
while (addNextPoint(grid, pointQueue)) {
45+
regions++;
46+
47+
while (!pointQueue.isEmpty()) {
48+
Point p = pointQueue.poll();
49+
50+
int x = p.x;
51+
int y = p.y;
52+
53+
// mark visited
54+
if (grid[x][y] == 1) {
55+
grid[x][y] = 2;
56+
57+
if (x < 127 && grid[x + 1][y] == 1) {
58+
pointQueue.add(new Point(x + 1, y));
59+
}
60+
61+
if (x > 0 && grid[x - 1][y] == 1) {
62+
pointQueue.add(new Point(x - 1, y));
63+
}
64+
65+
if (y < 127 && grid[x][y + 1] == 1) {
66+
pointQueue.add(new Point(x, y + 1));
67+
}
68+
69+
if (y > 0 && grid[x][y - 1] == 1) {
70+
pointQueue.add(new Point(x, y - 1));
71+
}
72+
}
73+
}
74+
}
75+
76+
return regions;
77+
}
78+
79+
private static boolean addNextPoint(byte[][] grid, Queue<Point> pointQueue) {
80+
for (int x = 0; x < 128; x++) {
81+
for (int y = 0; y < 128; y++) {
82+
if (grid[x][y] == 1) {
83+
pointQueue.add(new Point(x, y));
84+
return true;
85+
}
86+
}
87+
}
88+
return false;
89+
}
90+
91+
92+
static int part1(byte[] input1, int[] string, byte[] row) {
93+
byte[] input = new byte[input1.length + 5];
94+
byte[] suffix = {17, 31, 73, 47, 23};
95+
System.arraycopy(input1, 0, input, 0, input1.length);
96+
System.arraycopy(suffix, 0, input, input1.length, suffix.length);
97+
98+
int index = 0;
99+
int skipSize = 0;
100+
for (int r = 0; r < 64; r++) {
101+
for (int length : input) {
102+
for (int i = 0; i < length / 2; i++) {
103+
int n = (i + index) % string.length;
104+
int m = (length - i - 1 + index) % string.length;
105+
106+
int b = string[m];
107+
string[m] = string[n];
108+
string[n] = b;
109+
}
110+
index = (index + length + skipSize) % string.length;
111+
skipSize++;
112+
}
113+
}
114+
115+
int count = 0;
116+
byte[] hashes = new byte[16];
117+
for (int hashidx = 0; hashidx < 16; hashidx++) {
118+
for (int i = 0; i < 16; i++) {
119+
int a = string[16 * hashidx + i];
120+
hashes[hashidx] ^= a;
121+
}
122+
123+
for (int i = 0; i < 8; i++) {
124+
row[8 * hashidx + 7 - i] = (byte) ((hashes[hashidx] >> i) & 1);
125+
count += row[8 * hashidx + 7 - i];
126+
}
127+
}
128+
129+
return count;
130+
}
131+
132+
133+
public static void main(String[] args) {
134+
String input = "ffayrhll";
135+
// input = "flqrgnkx";
136+
System.out.println(part1(input));
137+
}
138+
}

0 commit comments

Comments
 (0)