Skip to content

Commit 039c0d2

Browse files
committed
feat: Solving day 24
1 parent 651ab98 commit 039c0d2

File tree

3 files changed

+628
-0
lines changed

3 files changed

+628
-0
lines changed

docs/day24.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
url: "https://adventofcode.com/2024/day/24"
3+
---
4+
5+
# Day 24: Crossed Wires
6+
7+
You and The Historians arrive at the edge of a large grove somewhere in the jungle. After the last incident, the Elves installed a small device that monitors the fruit. While The Historians search the grove, one of them asks if you can take a look at the monitoring device; apparently, it's been malfunctioning recently.
8+
9+
The device seems to be trying to produce a number through some boolean logic gates. Each gate has two inputs and one output. The gates all operate on values that are either true (`1`) or false (`0`).
10+
11+
* `AND` gates output `1` if both inputs are `1`; if either input is `0`, these gates output `0`.
12+
* `OR` gates output `1` if one or both inputs is `1`; if both inputs are `0`, these gates output `0`.
13+
* `XOR` gates output `1` if the inputs are different; if the inputs are the same, these gates output `0`.
14+
15+
Gates wait until both inputs are received before producing output; wires can carry `0`, `1` or no value at all. There are no loops; once a gate has determined its output, the output will not change until the whole system is reset. Each wire is connected to at most one gate output, but can be connected to many gate inputs.
16+
17+
Rather than risk getting shocked while tinkering with the live system, you write down all of the gate connections and initial wire values (your puzzle input) so you can consider them in relative safety. For example:
18+
19+
```txt
20+
x00: 1
21+
x01: 1
22+
x02: 1
23+
y00: 0
24+
y01: 1
25+
y02: 0
26+
27+
x00 AND y00 -> z00
28+
x01 XOR y01 -> z01
29+
x02 OR y02 -> z02
30+
```
31+
32+
Because gates wait for input, some wires need to start with a value (as inputs to the entire system). The first section specifies these values. For example, `x00: 1` means that the wire named `x00` starts with the value 1 (as if a gate is already outputting that value onto that wire).
33+
34+
The second section lists all of the gates and the wires connected to them. For example, `x00 AND y00 -> z00` describes an instance of an `AND` gate which has wires `x00` and `y00` connected to its inputs and which will write its output to wire `z00`.
35+
36+
In this example, simulating these gates eventually causes `0` to appear on wire `z00`, `0` to appear on wire `z01`, and `1` to appear on wire `z02`.
37+
38+
Ultimately, the system is trying to produce a number by combining the bits on all wires starting with `z`. `z00` is the least significant bit, then `z01`, then `z02`, and so on.
39+
40+
In this example, the three output bits form the binary number 100 which is equal to the decimal number `4`.
41+
42+
Here's a larger example:
43+
44+
```txt
45+
x00: 1
46+
x01: 0
47+
x02: 1
48+
x03: 1
49+
x04: 0
50+
y00: 1
51+
y01: 1
52+
y02: 1
53+
y03: 1
54+
y04: 1
55+
56+
ntg XOR fgs -> mjb
57+
y02 OR x01 -> tnw
58+
kwq OR kpj -> z05
59+
x00 OR x03 -> fst
60+
tgd XOR rvg -> z01
61+
vdt OR tnw -> bfw
62+
bfw AND frj -> z10
63+
ffh OR nrd -> bqk
64+
y00 AND y03 -> djm
65+
y03 OR y00 -> psh
66+
bqk OR frj -> z08
67+
tnw OR fst -> frj
68+
gnj AND tgd -> z11
69+
bfw XOR mjb -> z00
70+
x03 OR x00 -> vdt
71+
gnj AND wpb -> z02
72+
x04 AND y00 -> kjc
73+
djm OR pbm -> qhw
74+
nrd AND vdt -> hwm
75+
kjc AND fst -> rvg
76+
y04 OR y02 -> fgs
77+
y01 AND x02 -> pbm
78+
ntg OR kjc -> kwq
79+
psh XOR fgs -> tgd
80+
qhw XOR tgd -> z09
81+
pbm OR djm -> kpj
82+
x03 XOR y03 -> ffh
83+
x00 XOR y04 -> ntg
84+
bfw OR bqk -> z06
85+
nrd XOR fgs -> wpb
86+
frj XOR qhw -> z04
87+
bqk OR frj -> z07
88+
y03 OR x01 -> nrd
89+
hwm AND bqk -> z03
90+
tgd XOR rvg -> z12
91+
tnw OR pbm -> gnj
92+
```
93+
94+
After waiting for values on all wires starting with `z`, the wires in this system have the following values:
95+
96+
```txt
97+
bfw: 1
98+
bqk: 1
99+
djm: 1
100+
ffh: 0
101+
fgs: 1
102+
frj: 1
103+
fst: 1
104+
gnj: 1
105+
hwm: 1
106+
kjc: 0
107+
kpj: 1
108+
kwq: 0
109+
mjb: 1
110+
nrd: 1
111+
ntg: 0
112+
pbm: 1
113+
psh: 1
114+
qhw: 1
115+
rvg: 0
116+
tgd: 0
117+
tnw: 1
118+
vdt: 1
119+
wpb: 0
120+
z00: 0
121+
z01: 0
122+
z02: 0
123+
z03: 1
124+
z04: 0
125+
z05: 1
126+
z06: 1
127+
z07: 1
128+
z08: 1
129+
z09: 1
130+
z10: 1
131+
z11: 0
132+
z12: 0
133+
```
134+
135+
Combining the bits from all wires starting with `z` produces the binary number `0011111101000`. Converting this number to decimal produces `2024`.
136+
137+
Simulate the system of gates and wires. What decimal number does it output on the wires starting with `z`?

src/day24/day24.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package day24
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
const OUTPUT_WIRE_PREFIX = 'z'
9+
10+
type gate struct {
11+
typ string
12+
left, right string
13+
}
14+
15+
func (g gate) eval(left, right uint8) uint8 {
16+
switch g.typ {
17+
case "AND":
18+
return left & right
19+
case "OR":
20+
return left | right
21+
case "XOR":
22+
return left ^ right
23+
default:
24+
panic("Gate type could not be determined")
25+
}
26+
}
27+
28+
func Solve(input string) uint {
29+
wires, gates := parseInput(input)
30+
for {
31+
if len(gates) == 0 {
32+
break
33+
}
34+
35+
updates := 0
36+
for k, g := range gates {
37+
left, okL := wires[g.left]
38+
right, okR := wires[g.right]
39+
if okL && okR {
40+
wires[k] = g.eval(left, right)
41+
delete(gates, k)
42+
updates++
43+
}
44+
}
45+
46+
if updates == 0 {
47+
panic("Iterated through gates and could not make forward progress")
48+
}
49+
}
50+
return outputWire(wires)
51+
}
52+
53+
func parseInput(input string) (map[string]uint8, map[string]gate) {
54+
wires := make(map[string]uint8)
55+
gates := make(map[string]gate)
56+
parseGates := false
57+
for _, line := range strings.Split(input, "\n") {
58+
if line == "" {
59+
parseGates = true
60+
continue
61+
}
62+
63+
if parseGates {
64+
d := strings.Split(line, " ")
65+
if len(d) != 5 || d[3] != "->" {
66+
continue
67+
}
68+
69+
gates[d[4]] = gate{typ: d[1], left: d[0], right: d[2]}
70+
} else {
71+
d := strings.Split(line, ": ")
72+
if len(d) != 2 {
73+
continue
74+
}
75+
76+
var v uint8
77+
if d[1] == "1" {
78+
v = 1
79+
}
80+
wires[d[0]] = v
81+
}
82+
}
83+
return wires, gates
84+
}
85+
86+
func outputWire(wires map[string]uint8) uint {
87+
var result uint
88+
for k, w := range wires {
89+
if k[0] != OUTPUT_WIRE_PREFIX {
90+
continue
91+
}
92+
93+
pos, _ := strconv.Atoi(k[1:])
94+
result = result | uint(w)<<pos
95+
}
96+
return result
97+
}

0 commit comments

Comments
 (0)