|
| 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 | +} |
0 commit comments