Repository navigation
Expand file tree
/
Copy pathSporificaVirus_22.java
More file actions
159 lines (140 loc) · 4.7 KB
/
Copy pathSporificaVirus_22.java
File metadata and controls
159 lines (140 loc) · 4.7 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package adventofcode;
import java.util.*;
/**
* Why Did you create this class? what does it do?
*/
@SuppressWarnings("Duplicates")
public class SporificaVirus_22 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
if (s.trim().length() == 0) {
break;
}
builder.append(s).append("\n");
}
System.out.println(solve2(builder.toString()));
}
private static int solve(String s) {
if (s == null || s.length() == 0)
return 0;
String[] ar = s.split("\n");
int counter = 0;
int x = ar.length / 2;
int y = ar[0].length() / 2;
Map<Integer, Set<Integer>> infected = new HashMap<>();
for (int i = 0; i < ar.length; i++) {
infected.put(i, new HashSet<>());
for (int j = 0; j < ar[i].length(); j++) {
if (ar[i].charAt(j) == '#') {
infected.get(i).add(j);
}
}
}
int direction = 1; // 1 up, -1 down, 2 right, -2 left
for (int i = 0; i < 10_000; i++) {
boolean isInfected = infected.containsKey(x) && infected.get(x).contains(y);
if (isInfected) {
if (direction == 1) {
direction = 2;
} else if (direction == 2) {
direction = -1;
} else if (direction == -1) {
direction = -2;
} else {
direction = 1;
}
} else {
if (direction == 1) {
direction = -2;
} else if (direction == -2) {
direction = -1;
} else if (direction == -1) {
direction = 2;
} else {
direction = 1;
}
}
if (!isInfected) {
if (!infected.containsKey(x))
infected.put(x, new HashSet<>());
if (infected.get(x).add(y))
counter++;
} else {
infected.get(x).remove(y);
}
if (Math.abs(direction) == 1) {
x += -direction;
} else {
y += (direction > 0 ? 1 : -1);
}
}
return counter;
}
private static int solve2(String s) {
if (s == null || s.length() == 0)
return 0;
String[] ar = s.split("\n");
int counter = 0;
int x = ar.length / 2;
int y = ar[0].length() / 2;
Map<Integer, Map<Integer, Character>> map = new HashMap<>();
for (int i = 0; i < ar.length; i++) {
map.put(i, new HashMap<>());
for (int j = 0; j < ar[i].length(); j++) {
if (ar[i].charAt(j) == '#') {
map.get(i).put(j, 'i');
} else {
map.get(i).put(j, 'c');
}
}
}
int direction = 1; // 1 up, -1 down, 2 right, -2 left
for (int i = 0; i < 10_000_000; i++) {
char state = !map.containsKey(x) || !map.get(x).containsKey(y) ? 'c' : map.get(x).get(y);
if (state == 'i') {
if (direction == 1) {
direction = 2;
} else if (direction == 2) {
direction = -1;
} else if (direction == -1) {
direction = -2;
} else {
direction = 1;
}
} else if (state == 'c') {
if (direction == 1) {
direction = -2;
} else if (direction == -2) {
direction = -1;
} else if (direction == -1) {
direction = 2;
} else {
direction = 1;
}
} else if (state == 'f') {
direction *= -1;
}
if (!map.containsKey(x))
map.put(x, new HashMap<>());
if (state == 'c') {
map.get(x).put(y, 'w');
} else if (state == 'w') {
counter++;
map.get(x).put(y, 'i');
} else if (state == 'i') {
map.get(x).put(y, 'f');
} else {
map.get(x).put(y, 'c');
}
if (Math.abs(direction) == 1) {
x += -direction;
} else {
y += (direction > 0 ? 1 : -1);
}
}
return counter;
}
}