Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e9c1da

Browse files
day 24 part 1
1 parent 8fa35fe commit 8e9c1da

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.codefork.aoc2024.day24;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.regex.Pattern;
6+
import java.util.stream.Stream;
7+
8+
import static com.codefork.aoc2024.util.FoldLeft.foldLeft;
9+
10+
public record Device(Map<String, Wire> namesToWires) {
11+
12+
public sealed interface Wire permits And, Or, Xor, NamedValue, LiteralValue {
13+
int value(Device device);
14+
}
15+
16+
public record LiteralValue(int value) implements Wire {
17+
18+
@Override
19+
public int value(Device device) {
20+
return value;
21+
}
22+
}
23+
24+
public record NamedValue(String name) implements Wire {
25+
26+
@Override
27+
public int value(Device device) {
28+
return device.namesToWires().get(name).value(device);
29+
}
30+
}
31+
32+
public record And(NamedValue op1, NamedValue op2) implements Wire {
33+
34+
@Override
35+
public int value(Device device) {
36+
return op1.value(device) & op2.value(device);
37+
}
38+
}
39+
40+
public record Or(NamedValue op1, NamedValue op2) implements Wire {
41+
42+
@Override
43+
public int value(Device device) {
44+
return op1.value(device) | op2.value(device);
45+
}
46+
}
47+
48+
public record Xor(NamedValue op1, NamedValue op2) implements Wire {
49+
50+
@Override
51+
public int value(Device device) {
52+
return op1.value(device) ^ op2.value(device);
53+
}
54+
}
55+
56+
private static final Pattern literalValuePat = Pattern.compile("(\\w{3}): (\\d)");
57+
private static final Pattern gatePat = Pattern.compile("(\\w{3}) (AND|OR|XOR) (\\w{3}) -> (\\w{3})");
58+
59+
public static Device parse(Stream<String> data) {
60+
var namesToWires = data
61+
.filter(line -> !line.isEmpty())
62+
.collect(foldLeft(
63+
() -> new HashMap<String, Wire>(),
64+
(acc, line) -> {
65+
var literalValueMatcher = literalValuePat.matcher(line);
66+
if(literalValueMatcher.find()) {
67+
var name = literalValueMatcher.group(1);
68+
var value = Integer.parseInt(literalValueMatcher.group(2));
69+
acc.put(name, new LiteralValue(value));
70+
return acc;
71+
}
72+
var gateMatcher = gatePat.matcher(line);
73+
if(gateMatcher.find()) {
74+
var op1 = new NamedValue(gateMatcher.group(1));
75+
var op = gateMatcher.group(2);
76+
var op2 = new NamedValue(gateMatcher.group(3));
77+
var name = gateMatcher.group(4);
78+
var wire = switch(op) {
79+
case "AND" -> new And(op1, op2);
80+
case "OR" -> new Or(op1, op2);
81+
case "XOR" -> new Xor(op1, op2);
82+
default -> throw new RuntimeException("unrecognized gate: " + op);
83+
};
84+
acc.put(name, wire);
85+
return acc;
86+
}
87+
throw new RuntimeException("Couldn't parse line: " + line);
88+
})
89+
);
90+
return new Device(namesToWires);
91+
}
92+
93+
public int getWire(String wireName) {
94+
return namesToWires.get(wireName).value(this);
95+
}
96+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codefork.aoc2024.day24;
2+
3+
import com.codefork.aoc2024.Problem;
4+
import com.codefork.aoc2024.util.Assert;
5+
6+
import java.util.stream.Stream;
7+
8+
import static com.codefork.aoc2024.util.FoldLeft.foldLeft;
9+
10+
public class Part01 extends Problem {
11+
12+
public String solve(Stream<String> data) {
13+
var device = Device.parse(data);
14+
var zNames = device.namesToWires().keySet().stream()
15+
.filter(name -> name.startsWith("z"))
16+
.sorted()
17+
.toList()
18+
.reversed();
19+
var result = zNames.stream().collect(
20+
foldLeft(
21+
() -> 0L,
22+
(acc, name) -> (acc << 1) | device.getWire(name)
23+
)
24+
);
25+
return String.valueOf(result);
26+
}
27+
28+
@Override
29+
public String solve() {
30+
Assert.assertEquals("2024", solve(getSampleInput()));
31+
return solve(getInput());
32+
}
33+
34+
public static void main(String[] args) {
35+
new Part01().run();
36+
}
37+
38+
}

‎src/main/resources/day24/input

4.71 KB
Binary file not shown.

‎src/main/resources/day24/sample

760 Bytes
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /