Skip to content

Commit

Permalink
AoC 2024 Day 24 Part 2 - java
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 24, 2024
1 parent ebd8960 commit 033a3b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

## 2024

![](https://img.shields.io/badge/stars%20⭐-46-yellow)
![](https://img.shields.io/badge/days%20completed-23-red)
![](https://img.shields.io/badge/stars%20⭐-48-yellow)
![](https://img.shields.io/badge/days%20completed-24-red)

<!-- @BEGIN:ImplementationsTable:2024@ -->
| | 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 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | [](src/main/python/AoC2024_06.py) | [](src/main/python/AoC2024_07.py) | [](src/main/python/AoC2024_08.py) | [](src/main/python/AoC2024_09.py) | [](src/main/python/AoC2024_10.py) | [](src/main/python/AoC2024_11.py) | [](src/main/python/AoC2024_12.py) | [](src/main/python/AoC2024_13.py) | [](src/main/python/AoC2024_14.py) | [](src/main/python/AoC2024_15.py) | [](src/main/python/AoC2024_16.py) | [](src/main/python/AoC2024_17.py) | [](src/main/python/AoC2024_18.py) | [](src/main/python/AoC2024_19.py) | [](src/main/python/AoC2024_20.py) | [](src/main/python/AoC2024_21.py) | [](src/main/python/AoC2024_22.py) | [](src/main/python/AoC2024_23.py) | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | [](src/main/java/AoC2024_08.java) | | [](src/main/java/AoC2024_10.java) | [](src/main/java/AoC2024_11.java) | [](src/main/java/AoC2024_12.java) | | [](src/main/java/AoC2024_14.java) | [](src/main/java/AoC2024_15.java) | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | [](src/main/java/AoC2024_08.java) | | [](src/main/java/AoC2024_10.java) | [](src/main/java/AoC2024_11.java) | [](src/main/java/AoC2024_12.java) | | [](src/main/java/AoC2024_14.java) | [](src/main/java/AoC2024_15.java) | | | | | | | | | [](src/main/java/AoC2024_24.java) | |
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | [](src/main/rust/AoC2024_10/src/main.rs) | [](src/main/rust/AoC2024_11/src/main.rs) | [](src/main/rust/AoC2024_12/src/main.rs) | [](src/main/rust/AoC2024_13/src/main.rs) | [](src/main/rust/AoC2024_14/src/main.rs) | [](src/main/rust/AoC2024_15/src/main.rs) | [](src/main/rust/AoC2024_16/src/main.rs) | [](src/main/rust/AoC2024_17/src/main.rs) | [](src/main/rust/AoC2024_18/src/main.rs) | [](src/main/rust/AoC2024_19/src/main.rs) | [](src/main/rust/AoC2024_20/src/main.rs) | | [](src/main/rust/AoC2024_22/src/main.rs) | [](src/main/rust/AoC2024_23/src/main.rs) | | |
<!-- @END:ImplementationsTable:2024@ -->

Expand Down
58 changes: 44 additions & 14 deletions src/main/java/AoC2024_24.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import com.github.pareronia.aoc.StringOps;
import com.github.pareronia.aoc.StringOps.StringSplit;
Expand All @@ -15,7 +18,7 @@
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;

public class AoC2024_24 extends SolutionBase<List<AoC2024_24.Gate>, Long, Integer> {
public class AoC2024_24 extends SolutionBase<List<AoC2024_24.Gate>, Long, String> {

private AoC2024_24(final boolean debug) {
super(debug);
Expand All @@ -36,14 +39,13 @@ protected List<Gate> parseInput(final List<String> inputs) {

int part1(final List<Gate> inputs, final String wire) {
final Circuit circuit = Circuit.of(inputs);
log(circuit);
return circuit.getValue(wire);
}

@Override
public Long solvePart1(final List<Gate> inputs) {
String ans = "";
for (int i = 0; i <= 99; i++) {
for (int i = 0; i <= 45; i++) {
try {
ans = part1(inputs, String.format("z%02d", i)) + ans;
} catch (final AssertionError e) {
Expand All @@ -53,14 +55,45 @@ public Long solvePart1(final List<Gate> inputs) {
}

@Override
public Integer solvePart2(final List<Gate> inputs) {
// final List<Gate> clone = inputs.stream().map(Gate::cloneGate).collect(toList());
// final Circuit circuit1 = Circuit.of(inputs);
// final int a = circuit1.getValue("a");
// final Circuit circuit2 = Circuit.of(clone);
// circuit2.setGate("b", Gate.set("b", String.valueOf(a)));
// return circuit2.getValue("a");
return 0;
public String solvePart2(final List<Gate> inputs) {
final List<Gate> gates = Circuit.of(inputs).getGates().stream().filter(gate -> gate.op != AoC2024_24.Gate.Op.SET).toList();
final Set<String> swapped = new HashSet<>();
for (final Gate gate : gates) {
if (gate.op == AoC2024_24.Gate.Op.SET) {
continue;
}
if (gate.name.startsWith("z")
&& gate.op != AoC2024_24.Gate.Op.XOR
&& !gate.name.equals("z45")) {
swapped.add(gate.name);
}
if (gate.op == AoC2024_24.Gate.Op.XOR
&& Set.of(gate.name, gate.in1, gate.in2).stream()
.noneMatch(n -> n.startsWith("x")
|| n.startsWith("y")
|| n.startsWith("z"))) {
swapped.add(gate.name);
}
if (gate.op == AoC2024_24.Gate.Op.AND
&& !(gate.in1.equals("x00") || gate.in2.equals("x00"))) {
for (final Gate other : gates) {
if (other.op != AoC2024_24.Gate.Op.OR
&& (Set.of(other.in1, other.in2).contains(gate.name))) {
swapped.add(gate.name);
}
}
}
if (gate.op == AoC2024_24.Gate.Op.XOR) {
for (final Gate other : gates) {
if (other.op == AoC2024_24.Gate.Op.OR
&& (Set.of(other.in1, other.in2).contains(gate.name))) {
swapped.add(gate.name);
}
}
}
}
log(swapped);
return swapped.stream().sorted().collect(joining(","));
}

@Override
Expand Down Expand Up @@ -139,8 +172,6 @@ public static void main(final String[] args) throws Exception {

static final class Gate implements Cloneable {

private static final int BIT_SIZE = 16;

enum Op { SET, AND, OR, XOR }

final String name;
Expand Down Expand Up @@ -182,7 +213,6 @@ public static Gate fromInput(final String input) {
return Gate.or(splits[1], orSplits[0], orSplits[1]);
} else {
throw new IllegalArgumentException();
// return Gate.set(splits[1], splits[0]);
}
}

Expand Down

0 comments on commit 033a3b3

Please sign in to comment.