-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 24-2.java
185 lines (162 loc) · 5.38 KB
/
Day 24-2.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
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
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
class Main {
private static List<String> value = new ArrayList<String>();
private static List<List<Integer>> black = new ArrayList<List<Integer>>();
private static HashMap<List<Integer>, Integer> map = new HashMap<List<Integer>, Integer>();
public static void main(String[] args) {
populateArray();
System.out.println(value);
firstStar();
System.out.println(black.size());
System.out.println(black);
SecondStar();
System.out.println("blacks: " + black.size());
}
public static void firstStar(){
for (int i = 0; i < value.size(); i++){
String currValue = value.get(i);
//System.out.println("curr value: " + currValue);
Integer[] location = new Integer[2];
location[0] = 0;
location[1] = 0;
for (int j = 0; j < currValue.length();){
String single = currValue.substring(j, j+1);
String twosubstring = "";
boolean isthere = true;
if (j < currValue.length()-1){
twosubstring = currValue.substring(j, j+2);
} else {
isthere = false;
}
if (!twosubstring.equals("")){
if (twosubstring.equals("se")){
location[1]-=2;
location[0]++;
//System.out.println(twosubstring);
} else if (twosubstring.equals("sw")){
location[1]-=2;
location[0]--;
//System.out.println(twosubstring);
} else if (twosubstring.equals("nw")){
location[1]+=2;
location[0]--;
//System.out.println(twosubstring);
} else if (twosubstring.equals("ne")){
location[1]+=2;
location[0]++;
//System.out.println(twosubstring);
} else {
isthere = false;
}
}
if (!isthere){
j++;
if (single.equals("e")){
location[0]+=2;
} else if (single.equals("w")){
location[0]-=2;
}
//System.out.println(single);
} else {
j+=2;
}
//System.out.println(Arrays.toString(location));
}
//System.out.println(Arrays.toString(location));
//System.out.println(black.indexOf(Arrays.asList(location)));
if (black.contains(Arrays.asList(location))){
System.out.println("contains");
black.remove(black.indexOf(Arrays.asList(location)));
} else {
black.add(Arrays.asList(location));
}
}
for (int i = 0; i < black.size(); i++){
map.put(black.get(i),1);
}
//System.out.println("size: " + black.size());
}
private static int[][] direction = new int[][]{{1,-2}, {-1,-2}, {-2,0}, {-1,2}, {1, 2}, {2, 0}};
public static Integer surrounding(int x, int y){
int count = 0;
for (int[] coordinate : direction){
List<Integer> temp = new ArrayList<Integer>();
int cx = x + coordinate[0];
int cy = y + coordinate[1];
temp.add(cx);
temp.add(cy);
if (black.contains(temp)){
count++;
}
}
return count;
}
public static void SecondStar(){
for (int m = 0; m < 100; m++){
List<List<Integer>> newBlack = new ArrayList<List<Integer>>();
List<List<Integer>> newWhite = new ArrayList<List<Integer>>();
int maxY = 0;
int maxX = 0;
int leastX = 0;
int leastY = 0;
for (int i = 0; i < black.size(); i++){
List<Integer> value = black.get(i);
if (value.get(0) > maxX){
maxX = value.get(0);
} else if (value.get(0) < leastX){
leastX = value.get(0);
}
if (value.get(1) > maxY){
maxY = value.get(1);
} else if (value.get(1) < leastY){
leastY = value.get(1);
}
}
for (int i = leastX -4; i < maxX + 4; i++){
for (int j = leastY -4; j < maxY + 4; j+=2){
List<Integer> value = new ArrayList<Integer>();
value.add(i);
value.add(j);
int surround = surrounding(i , j);
if ((surround == 0 || surround > 2) && black.contains(value)){
//System.out.println(value + " is now white");
newWhite.add(value);
} else if (surround == 2 && !black.contains(value)){
//System.out.println(value + " is now black");
newBlack.add(value);
}
}
}
//System.out.println("removing...");
for (List<Integer> inside : newWhite){
//System.out.println(inside);
black.remove(black.indexOf(inside));
}
//System.out.println(black);
//System.out.println("adding...");
for (List<Integer> inside : newBlack){
//System.out.println(inside);
black.add(inside);
}
//System.out.println("new black size: " + newBlack.size());
System.out.println("m is: " + m + " black is " + black.size());
}
}
public static void populateArray(){
try {
File myObj = new File("puzzle.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
value.add(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}