This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lobby_layout.java
99 lines (94 loc) · 3.42 KB
/
lobby_layout.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class lobby_layout {
public final static ArrayList<String> values = new ArrayList<>();
public final static Map<String, List<Integer>> hexagons = new HashMap<>();
public final static Map<List<Integer>, Boolean> tiles = new HashMap<>();
public final static List<List<String>> directions = new ArrayList<>();
public static int solve_part_one() {
for (String line : values) {
line = line.replace("e", "e ").replace("w", "w ");
directions.add(Arrays.asList(line.split(" ")));
}
for (List<String> d : directions) {
List<Integer> c = new ArrayList(Arrays.asList(0, 0));
for (String m : d) {
c.set(0, c.get(0) + hexagons.get(m).get(0));
c.set(1, c.get(1) + hexagons.get(m).get(1));
}
tiles.put(c, !tiles.getOrDefault(c, false));
}
int result = 0;
for (Boolean v : tiles.values()) {
if (v) result++;
}
return result;
}
public static int solve_part_two() {
Set<List<Integer>> black = new HashSet<>();
for (List<Integer> pos : tiles.keySet()) {
if (tiles.get(pos)) {
black.add(pos);
}
}
for (int i = 0; i < 100; i++) {
Set<List<Integer>> all = new HashSet<>();
Set<List<Integer>> toRemove = new HashSet<>();
Set<List<Integer>> toAdd = new HashSet<>();
for (List<Integer> c : black) {
int sum = 0;
for (List<Integer> dir : hexagons.values()) {
int x = c.get(0) + dir.get(0);
int y = c.get(1) + dir.get(1);
all.add(Arrays.asList(x, y));
if (black.contains(Arrays.asList(x, y))) {
sum++;
}
}
if (sum < 1 || sum > 2) {
toRemove.add(c);
}
}
for (List<Integer> c : all) {
int sum = 0;
for (List<Integer> dir : hexagons.values()) {
int x = c.get(0) + dir.get(0);
int y = c.get(1) + dir.get(1);
if (black.contains(Arrays.asList(x, y))) {
sum++;
}
}
if (sum == 2) {
toAdd.add(c);
}
}
black.removeAll(toRemove);
black.addAll(toAdd);
}
return black.size();
}
public static void main (String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("input.txt"));
String line;
while ((line = reader.readLine()) != null) {
values.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
hexagons.put("nw", Arrays.asList(-1, 1));
hexagons.put("sw", Arrays.asList(-1,-1));
hexagons.put("ne", Arrays.asList( 1, 1));
hexagons.put("se", Arrays.asList( 1,-1));
hexagons.put("e" , Arrays.asList( 2, 0));
hexagons.put("w" , Arrays.asList(-2, 0));
System.out.println(solve_part_one());
System.out.println(solve_part_two());
}
}