Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
- Advent of Code 2024 Day 21
- Advent of Code 2024 Day 22
- Advent of Code 2024 Day 23
- Advent of Code 2024 Day 24
- Advent of Code 2024 Day 25
-
Older post
-
Newer post
Advent of Code 2024 Day 24
Day 24: Crossed Wires
https://adventofcode.com/2024/day/24
Another day, another familiar location.
Some device is acting up and you need to fix it (but thankfully, it’s not a printer!). If works on a bunch of boolean logic.
An example input looks like this:
The top part is a bunch of wires with the boolean value they currently hold.
eg. the wire called x00
is set to true
, and y02
is set to false
.
The bottom part describes the logic gates in the device.
Parsing
I could throw everything in a string and work with those, I could, but I didn’t.
Part 1
After the logic gates do their thing completely (there are no loops).
The system is trying to procude a number by combining the bits on all wires starting with a z
.
z00
is the least significant bit.
The question asks what that number is in decimal.
Helpers
Adding a method to any Operator
that actually does that bit operation.
Now the actually interesting bit, the functon that recursively calculates the boolean on a given wire given the starting booleans on the wires and a map of all logic operations.
Code
- Gather all wires starting with a
z
- Order them in reverse
- Calculate their boolean value
- Concatenate them
- Turn the resulting binary number into decimal
Part 2
You know that first part of the input you parsed? Throw it away, it’s useless in part2!
The system is trying to add 2 binary numbers.
- the bits on wires starting with
x
form a binary number. - the bits on wires starting with
y
form a binary number. - Those 2 number are (binary) added together and the resulting bits are output to wires starting with
z
Again, 00
is the least significant bit, then 01
, then 02
, …
It’s wrong because 4 pairs of wires are swapped.
The question asks to print all wires that are wrong, sort them, and put a comma between them.
The code I used is Hyperneutrino’s code translated to Rust. For an explanation of the logic it performs please watch her video.
Basically, it starts a counter at 0
and checks how high it can go before a z
wire with that number is wrong.
It then bruteforce swaps wires and performs that check again for each swap.
If the counter gets higher, it keeps that swap.
The interesting parts are the functions that check if a z
wire has the correct output boolean.