Skip to content

Commit cbd2e23

Browse files
author
Sherali Obidov
committed
finally 18 is over, ignorance is not bliss. just skipped small detail and spent 10 hours solving problem ))
1 parent be11bc1 commit cbd2e23

2 files changed

Lines changed: 223 additions & 5 deletions

File tree

‎src/adventofcode/Duet_18.java‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package adventofcode;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Why Did you create this class? what does it do?
7+
*/
8+
@SuppressWarnings("Duplicates") public class Duet_18 {
9+
public static void main(String[] args) {
10+
Scanner scanner = new Scanner(System.in);
11+
StringBuilder builder = new StringBuilder();
12+
13+
while (scanner.hasNextLine()) {
14+
String s = scanner.nextLine();
15+
if (s.equals("1"))
16+
break;
17+
builder.append(s).append("\n");
18+
}
19+
20+
//System.out.println(s0lve(builder.toString()));
21+
Duet_18 duet = new Duet_18();
22+
System.out.println(duet.s0lve2(builder.toString()));
23+
24+
}
25+
26+
private static long s0lve(String s) {
27+
if (s == null || s.length() == 0l)
28+
return -1;
29+
String[] a = s.split("\n");
30+
Map<String, Long> map = new HashMap<>();
31+
long lastPlayed = 0;
32+
for (int i = 0; i < a.length; i++) {
33+
String current = a[i];
34+
String[] aa = current.split(" ");
35+
if (aa[0].equals("rcv")) {
36+
if (Character.isDigit(aa[1].charAt(0)) && Integer.parseInt(aa[1]) != 0) {
37+
return lastPlayed;
38+
} else if (map.get(aa[1]) != 0) {
39+
return lastPlayed;
40+
}
41+
} else if (aa[0].equals("snd")) {
42+
if (Character.isAlphabetic(aa[1].charAt(0))) {
43+
lastPlayed = map.get(aa[2]);
44+
} else {
45+
lastPlayed = Integer.parseInt(aa[1]);
46+
}
47+
} else if (aa[0].equals("set")) {
48+
if (Character.isAlphabetic(aa[2].charAt(0))) {
49+
map.put(aa[1], map.getOrDefault(aa[2], 0l));
50+
} else {
51+
map.put(aa[1], Long.parseLong(aa[2]));
52+
}
53+
} else if (aa[0].equals("add")) {
54+
if (Character.isAlphabetic(aa[2].charAt(0))) {
55+
map.put(aa[1], map.get(aa[1]) + map.get(aa[2]));
56+
} else {
57+
map.put(aa[1], map.get(aa[1]) + Long.parseLong(aa[2]));
58+
}
59+
} else if (aa[0].equals("mul")) {
60+
if (Character.isAlphabetic(aa[2].charAt(0))) {
61+
map.put(aa[1], map.get(aa[1]) * map.get(aa[2]));
62+
} else {
63+
map.put(aa[1], map.get(aa[1]) * Long.parseLong(aa[2]));
64+
}
65+
} else if (aa[0].equals("mod")) {
66+
if (Character.isAlphabetic(aa[2].charAt(0))) {
67+
map.put(aa[1], map.get(aa[1]) % map.get(aa[2]));
68+
} else {
69+
map.put(aa[1], map.get(aa[1]) % Long.parseLong(aa[2]));
70+
}
71+
} else if (aa[0].equals("jgz")) {
72+
if (map.get(aa[1]) <= 0L)
73+
continue;
74+
if (Character.isAlphabetic(aa[2].charAt(0))) {
75+
i += map.get(aa[2]);
76+
} else {
77+
i += Integer.parseInt(aa[2]);
78+
}
79+
i -= 1;
80+
}
81+
}
82+
return 0;
83+
}
84+
85+
int counter1 = 0;
86+
87+
public long s0lve2(String s) {
88+
if (s == null || s.length() == 0) {
89+
return -1;
90+
}
91+
String[] a = s.split("\n");
92+
long[] map0 = new long[128];
93+
long[] map1 = new long[128];
94+
map0['p'] = 0;
95+
map1['p'] = 1;
96+
Queue<Long> queue0 = new LinkedList<>();
97+
Queue<Long> queue1 = new LinkedList<>();
98+
int p0 = 0;
99+
int p1 = 0;
100+
while (true) {
101+
102+
p0 = helper(map0, queue0, queue1, p0, a, true);
103+
p1 = helper(map1, queue1, queue0, p1, a, false);
104+
105+
if ((p0 == -1 && p1 == -1) || (queue0.size() == 0 && queue1.size() == 0)) {
106+
return counter1;
107+
}
108+
}
109+
}
110+
111+
int helper(long[] map, Queue<Long> myQueue, Queue<Long> anotherQueue, int k, String[] a, boolean b) {
112+
for (int i = k; i < a.length; i++) {
113+
char index = a[i].charAt(4);
114+
if (a[i].startsWith("rcv")) {
115+
if (myQueue.isEmpty()) {
116+
return i;
117+
}
118+
map[index] = myQueue.remove();
119+
} else if (a[i].startsWith("snd")) {
120+
anotherQueue.add(map[index]);
121+
if (!b) {
122+
counter1++;
123+
}
124+
} else if (a[i].startsWith("set")) {
125+
if (Character.isAlphabetic(a[i].charAt(6))) {
126+
map[index] = map[a[i].charAt(6)];
127+
} else {
128+
map[index] = Long.parseLong(a[i].substring(6));
129+
}
130+
} else if (a[i].startsWith("add")) {
131+
if (Character.isAlphabetic(a[i].charAt(6))) {
132+
map[index] += map[a[i].charAt(6)];
133+
} else {
134+
map[index] += Long.parseLong(a[i].substring(6));
135+
}
136+
} else if (a[i].startsWith("mul")) {
137+
if (Character.isAlphabetic(a[i].charAt(6))) {
138+
map[index] *= map[a[i].charAt(6)];
139+
} else {
140+
map[index] *= Long.parseLong(a[i].substring(6));
141+
}
142+
} else if (a[i].startsWith("mod")) {
143+
if (Character.isAlphabetic(a[i].charAt(6))) {
144+
map[index] %= map[a[i].charAt(6)];
145+
} else {
146+
map[index] %= Long.parseLong(a[i].substring(6));
147+
}
148+
} else if (a[i].startsWith("jgz")) {
149+
if ((Character.isDigit(index) && index - '0' <= 0) ||
150+
(Character.isAlphabetic(index) && map[index] <= 0)) {
151+
continue;
152+
}
153+
if (Character.isAlphabetic(a[i].charAt(6))) {
154+
i += map[a[i].charAt(6)];
155+
} else {
156+
i += Integer.parseInt(a[i].substring(6));
157+
}
158+
i -= 1;
159+
}
160+
}
161+
return -1;
162+
}
163+
164+
}

‎src/adventofcode/HexEd_11.java‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,70 @@ public class HexEd_11 {
99
public static void main(String[] args) {
1010
Scanner scanner = new Scanner(System.in);
1111
StringBuilder builder = new StringBuilder();
12+
1213
while (scanner.hasNextLine()) {
1314
String s = scanner.nextLine();
1415
if (s.equals("1"))
1516
break;
1617
builder.append(s);
1718
}
18-
System.out.println(solve(builder));
19+
System.out.println(solve1(builder));
20+
}
21+
22+
private static int solve1(StringBuilder builder) {
23+
if (builder == null || builder.length() <= 1)
24+
return 0;
25+
String[] a = builder.toString().split(",");
26+
Map<String, Integer> map = new HashMap<>();
27+
for (int i = 0; i < a.length; i++) {
28+
String current = a[i];
29+
String opp = "";
30+
switch (current) {
31+
case "n":
32+
opp = "s";
33+
break;
34+
case "s":
35+
opp = "n";
36+
break;
37+
case "ne":
38+
opp = "sw";
39+
break;
40+
case "se":
41+
opp = "nw";
42+
break;
43+
case "sw":
44+
opp = "ne";
45+
break;
46+
case "nw":
47+
opp = "se";
48+
break;
49+
}
50+
if (!map.containsKey(current))
51+
map.put(current, 0);
52+
if (!map.containsKey(opp))
53+
map.put(opp, 0);
54+
55+
if (map.get(opp) == 0) {
56+
map.put(current, map.get(current) + 1);
57+
} else {
58+
map.put(opp, map.get(opp) - 1);
59+
}
60+
}
61+
System.out.println(map);
62+
return 0;
1963
}
2064

2165
private static int solve(StringBuilder builder) {
2266
if (builder == null || builder.length() <= 1)
2367
return 0;
2468
String[] a = builder.toString().split(",");
25-
int dest = 0;
69+
int dest = 10_000;
2670
for (int i = 0; i < a.length; i++) {
2771
String current = a[i];
2872
if (current.length() == 1) {
2973
dest += (current.equals("n") ? -10 : 10);
3074
} else {
31-
if (dest % 2 == 0) {
75+
if (dest % 2 != 0) {
3276
if (current.equals("ne"))
3377
dest -= 9;
3478
else if (current.equals("se"))
@@ -56,10 +100,18 @@ else if (current.equals("nw"))
56100
queue.add(0);
57101
visited.add(0);
58102
Map<Integer, Integer> counts = new HashMap<>();
103+
Map<Integer, Integer> parent = new HashMap<>();
59104
counts.put(0, 0);
105+
parent.put(0, 0);
60106
while (!queue.isEmpty()) {
61107
Integer current = queue.remove();
62-
if (current == dest) {
108+
if (current == dest - 10_000) {
109+
int key = 8;
110+
while (key != 0) {
111+
System.out.print(key + " => ");
112+
key = parent.get(key);
113+
}
114+
System.out.println();
63115
return counts.get(current);
64116
}
65117
generateNeighbors(map, current);
@@ -69,17 +121,19 @@ else if (current.equals("nw"))
69121
visited.add(nei);
70122
queue.add(nei);
71123
counts.put(nei, counts.get(current) + 1);
124+
parent.put(nei, current);
72125
}
73126
}
74127
}
128+
75129
return counts.get(dest);
76130
}
77131

78132
private static void generateNeighbors(Map<Integer, Set<Integer>> map, Integer current) {
79133
Set<Integer> set = new HashSet<>();
80134
set.add(current + 10);
81135
set.add(current - 10);
82-
if (current % 2 == 0) {
136+
if (current % 2 != 0) {
83137
set.add(current - 9);
84138
set.add(current + 1);
85139
set.add(current - 1);

0 commit comments

Comments
 (0)