-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay08.java
More file actions
154 lines (141 loc) · 5.86 KB
/
Day08.java
File metadata and controls
154 lines (141 loc) · 5.86 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
//class Directions used in both parts
package org.thehuglio;
import java.util.LinkedList;
import java.util.List;
public class Directions {
private final List<String> current = new LinkedList<>();
private final List<String> left = new LinkedList<>();
private final List<String> right = new LinkedList<>();
public void setDirections(String current,String left,String right) {
this.current.add(current);
this.left.add(left);
this.right.add(right);
}
public String getDirections(String current,String leftright) {
if (leftright.equals("L")) {
return left.get(this.current.indexOf(current));
} else {
return right.get(this.current.indexOf(current));
}
}
public Boolean currentcontains(String s) {
return current.contains(s);
}
public int currentsize() {
return current.size();
}
}
//part 1
package org.thehuglio;
import java.io.File;
import java.util.*;
public class Main {
private static final List<String> data = new Reader(new File("data.txt")).data;
public static List<Integer> sorted = new LinkedList<>();
public static void main(String[] args) {
Directions directions = new Directions();
for (int i = 2; i < data.size(); i++) {
if (!data.get(i).equals("")) {
String[] split = data.get(i).split(" = ");
directions.setDirections(split[0], split[1].split(", ")[0].replace("(", ""), split[1].split(", ")[1].replace(")", ""));
}
}
String[] rl = data.get(0).split("");
String current = "AAA";
int tot = 0;
while (!current.equals("ZZZ")) {
for (String direction : rl) {
current = directions.getDirections(current,direction);
tot++;
}
}
System.out.println(tot);
}
}
//part 2 (not finished
package org.thehuglio;
import java.io.File;
import java.util.*;
public class Main {
private static final List<String> data = new Reader(new File("data.txt")).data;
public static List<Integer> sorted = new LinkedList<>();
public static void main(String[] args) {
Directions directions = new Directions();
List<String> current = new LinkedList<>();
for (int i = 2; i < data.size(); i++) {
if (!data.get(i).equals("")) {
String[] split = data.get(i).split(" = ");
directions.setDirections(split[0], split[1].split(", ")[0].replace("(", ""), split[1].split(", ")[1].replace(")", ""));
if (split[0].endsWith("A")) {
current.add(split[0]);
}
}
}
String[] rl = data.get(0).split("");
int tot = 0;
boolean check = true;
List<List<Integer>> loops = new LinkedList<>();
List<Integer> done = new LinkedList<>();
while (check) {
for (String direction : rl) {
List<String> temp = new LinkedList<>();
for (String s : current) {
temp.add(directions.getDirections(s, direction));
}
tot++;
for (int i = 0; i < temp.size(); i++) {
if (!done.contains(i)) {
if (temp.get(i).endsWith("Z")) {
int tempi = 0;
if (loops.size() <= i) {
loops.add(List.of(tot));
} else {
for (int j : loops.get(i)) {
tempi += j;
}
int t = tot - tempi;
if (loops.get(i).contains(t)) {
done.add(i);
} else {
List<Integer> templist = new LinkedList<>(loops.get(i));
templist.add(t);
loops.set(i, templist);
}
}
}
check = true;
}
if (done.size() == current.size()) {
List<Long> lastlist = new LinkedList<>();
for (List<Integer> loop : loops) {
lastlist.add(Long.valueOf(loop.get(0)));
}
List<Integer> counter = new LinkedList<>(List.of(0,0,0,0,0,0,0));
while (true) {
for (int j = 0; j < loops.size() - 1; j++) {
List<Integer> loop = loops.get(j);
if (!Objects.equals(lastlist.get(j), lastlist.get(j + 1))) {
if (lastlist.get(j) < lastlist.get(j + 1)) {
lastlist.set(j, lastlist.get(j) + loop.get(counter.get(j) % (loop.size() - 1) + 1));
counter.set(j, counter.get(j) + 1);
} else {
lastlist.set(j, lastlist.get(j+1) + loop.get(counter.get(j+1) % (loop.size() - 1) + 1));
counter.set(j, counter.get(j+1) + 1);
}
break;
}
}
if (Objects.equals(lastlist.get(1), lastlist.get(4))) {
System.out.println(lastlist);
check = false;
break;
}
}
}
}
current = temp;
if (!check) { break; }
}
}
}
}