Skip to content

Commit

Permalink
cleanup?
Browse files Browse the repository at this point in the history
  • Loading branch information
zebalu committed Dec 22, 2023
1 parent 11c388f commit 6a224eb
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day20.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ private static long part2(Machine machine) {
while (!machine.push()) {
++counter;
}
broadcaster.outputs = originalOutputs;
machine.reset();
result = lcm(result, counter);
}
broadcaster.outputs = originalOutputs;
return result;
}

Expand All @@ -45,11 +45,7 @@ private static long lcm(long a, long b) {
}

private static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
return b == 0 ? a : gcd(b, a % b);
}

private static String readInput() {
Expand Down Expand Up @@ -84,9 +80,9 @@ private static class Node {
static Node parse(String line) {
Node result = new Node();
var parts = line.split(" -> ");
if (parts[0].equals("broadcaster")) {
if (parts[0].equals(BROADCASTER)) {
result.type = '?';
result.name = parts[0];
result.name = BROADCASTER;
} else {
result.type = parts[0].charAt(0);
result.name = parts[0].substring(1);
Expand All @@ -104,26 +100,25 @@ void markInput(String input) {
}

List<Message> recieve(String from, Pulse pulse) {
List<Message> notified = new ArrayList<>();
switch (type) {
return switch (type) {
case '%' -> {
if (pulse == Pulse.LOW) {
onOff = !onOff;
outputs.stream().map(s -> new Message(name, s, onOff ? Pulse.HIGH : Pulse.LOW))
.forEach(notified::add);
}
yield switch(pulse) {
case LOW -> { onOff = !onOff; yield forward(onOff ? Pulse.HIGH : Pulse.LOW); }
case HIGH -> List.of();
};
}
case '&' -> {
inputs.put(from, pulse);
boolean allHigh = inputs.values().stream().allMatch(p -> p == Pulse.HIGH);
outputs.stream().map(s -> new Message(name, s, allHigh ? Pulse.LOW : Pulse.HIGH))
.forEach(notified::add);
}
case '?' -> {
outputs.stream().map(s -> new Message(name, s, pulse)).forEach(notified::add);
yield forward(allHigh ? Pulse.LOW : Pulse.HIGH);
}
}
return notified;
case '?' -> forward(pulse);
default -> throw new IllegalStateException("unkown node type: " + type);
};
}

private List<Message> forward(Pulse pulse) {
return outputs.stream().map(s -> new Message(name, s, pulse)).toList();
}

void reset() {
Expand Down

0 comments on commit 6a224eb

Please sign in to comment.