Skip to content
Snippets Groups Projects
Commit 00fd3876 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Day 16 complete

parent f59db645
Branches
No related tags found
No related merge requests found
...@@ -216,4 +216,6 @@ relationships between the tiles, though. ...@@ -216,4 +216,6 @@ relationships between the tiles, though.
## Day 16 ## Day 16
Part 1 looks to be about preparing this year's computer's syntax tree. I think Part 1 looks to be about preparing this year's computer's syntax tree. I think
I'll try out Java's new "sealed class." I'll try out Java's new "sealed class." Part 2 isn't particularly challenging:
\ No newline at end of file the hard part was setting up the syntax tree in Part 1. I'll get to use Java's
new switch expressions, though.
...@@ -28,7 +28,15 @@ public class Day16 extends Puzzle { ...@@ -28,7 +28,15 @@ public class Day16 extends Puzzle {
// sampleData = "8A004A801A8002F478"; // version sum = 16 // sampleData = "8A004A801A8002F478"; // version sum = 16
// sampleData = "620080001611562C8802118E34"; // version sum = 12 // sampleData = "620080001611562C8802118E34"; // version sum = 12
// sampleData = "C0015000016115A2E0802F182340"; // version sum = 23 // sampleData = "C0015000016115A2E0802F182340"; // version sum = 23
sampleData = "A0016C880162017C3686B18A3D4780"; // version sum = 31 // sampleData = "A0016C880162017C3686B18A3D4780"; // version sum = 31
// sampleData = "C200B40A82"; // 1 + 2 = 3
// sampleData = "04005AC33890"; // 6 * 9 = 54
// sampleData = "880086C3E88112"; // min(7,8,9) = 7
// sampleData = "CE00C43D881120"; // max(7,8,9) = 9
// sampleData = "D8005AC2A8F0"; // (5 < 15) = 1
// sampleData = "F600BC2D8F"; // (5 > 15) = 0
// sampleData = "9C005AC2F8F0"; // (5 == 15) = 0
sampleData = "9C0141080250320F1802104A08"; // (1+3 == 2*2) = 1
} }
@Override @Override
...@@ -39,6 +47,7 @@ public class Day16 extends Puzzle { ...@@ -39,6 +47,7 @@ public class Day16 extends Puzzle {
@Override @Override
public long computePart2(List<String> data) { public long computePart2(List<String> data) {
return 0; Computer computer = new Computer(data.get(0));
return computer.compute();
} }
} }
...@@ -31,4 +31,8 @@ public class Computer { ...@@ -31,4 +31,8 @@ public class Computer {
public long addPacketVersionNumbers() { public long addPacketVersionNumbers() {
return syntaxTree.addPacketVersionNumbers(); return syntaxTree.addPacketVersionNumbers();
} }
public long compute() {
return syntaxTree.compute();
}
} }
...@@ -22,4 +22,9 @@ final class LiteralValuePacket extends Packet { ...@@ -22,4 +22,9 @@ final class LiteralValuePacket extends Packet {
public long addPacketVersionNumbers() { public long addPacketVersionNumbers() {
return version; return version;
} }
@Override
public long compute() {
return value;
}
} }
...@@ -30,4 +30,18 @@ public final class OperatorPacket extends Packet { ...@@ -30,4 +30,18 @@ public final class OperatorPacket extends Packet {
public long addPacketVersionNumbers() { public long addPacketVersionNumbers() {
return version + subPackets.stream().mapToLong(Packet::addPacketVersionNumbers).sum(); return version + subPackets.stream().mapToLong(Packet::addPacketVersionNumbers).sum();
} }
@Override
public long compute() {
return switch ((int)typeID) {
case 0 -> subPackets.stream().mapToLong(Packet::compute).sum();
case 1 -> subPackets.stream().mapToLong(Packet::compute).reduce((a, b) -> a * b).orElseThrow();
case 2 -> subPackets.stream().mapToLong(Packet::compute).min().orElseThrow();
case 3 -> subPackets.stream().mapToLong(Packet::compute).max().orElseThrow();
case 5 -> subPackets.get(0).compute() > subPackets.get(1).compute() ? 1 : 0;
case 6 -> subPackets.get(0).compute() < subPackets.get(1).compute() ? 1 : 0;
case 7 -> subPackets.get(0).compute() == subPackets.get(1).compute() ? 1 : 0;
default -> throw new IllegalStateException("Unknown type ID: " + typeID);
};
}
} }
...@@ -39,4 +39,6 @@ public abstract sealed class Packet permits LiteralValuePacket, OperatorPacket { ...@@ -39,4 +39,6 @@ public abstract sealed class Packet permits LiteralValuePacket, OperatorPacket {
} }
public abstract long addPacketVersionNumbers(); public abstract long addPacketVersionNumbers();
public abstract long compute();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment