-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
192 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## --- Day 24: Crossed Wires --- | ||
You and The Historians arrive at the edge of a [large grove](/2022/day/23) 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. | ||
|
||
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 <em>true</em> (<code>1</code>) or <em>false</em> (<code>0</code>). | ||
|
||
_Visit the website for the full story and [full puzzle](https://adventofcode.com/2024/day/24) description._ | ||
|
||
The first half of the problem was a familiar logic circuit evaluator, we have done this before. I really liked the second part, although I don't have a super generic solution for it. I generated a graph from my input using Graphviz, then realized that the circuit tries to implement a full adder. | ||
|
||
A single full adder consists of two half adders: | ||
|
||
![half adder](halfadder.png) | ||
|
||
Which are chained one after the other like this: | ||
|
||
![full adder](adder.png) | ||
|
||
I took the images from [build-electronic-circuits.com](https://www.build-electronic-circuits.com/full-adder/). | ||
|
||
I implemented this logic in my 'fix' method. I start with the output x01 and y01 and try to identify the gates for the individual steps. Where I found an error, I manually checked my input to figure out what went wrong. I had just two different | ||
kind of errors which can be corrected by the fixer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
namespace AdventOfCode.Y2024.Day24; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
record struct Rule(string in1, string in2, string kind, string output); | ||
|
||
[ProblemName("Crossed Wires")] | ||
class Solution : Solver { | ||
|
||
public object PartOne(string input) { | ||
var gate = Parse(input); | ||
var bits = gate.Keys.Where(k => k.StartsWith("z")).OrderByDescending(x => x).ToArray(); | ||
var res = 0L; | ||
foreach (var b in bits) { | ||
res = res * 2 + gate[b](); | ||
} | ||
return res; | ||
} | ||
|
||
public object PartTwo(string input) { | ||
var swaps = Fix(ParseRules(input.Split("\n\n")[1])); | ||
return string.Join(",", swaps.OrderBy(x => x)); | ||
} | ||
|
||
// the rules should define a full adder for two 44 bit numbers | ||
// this fixer is specific to my input. | ||
IEnumerable<string> Fix(List<Rule> rules) { | ||
var cin = Output(rules, "x00", "AND", "y00"); | ||
for (var i = 1; i < 45; i++) { | ||
var x = $"x{i:D2}"; | ||
var y = $"y{i:D2}"; | ||
var z = $"z{i:D2}"; | ||
|
||
var xor1 = Output(rules, x, "XOR", y); | ||
var and1 = Output(rules, x, "AND", y); | ||
|
||
var and2 = Output(rules, cin, "AND", xor1); | ||
var xor2 = Output(rules, cin, "XOR", xor1); | ||
|
||
if (xor2 == null && and2 == null) { | ||
return Swap(rules, xor1, and1); | ||
} | ||
|
||
var carry = Output(rules, and1, "OR", and2); | ||
if (xor2 != z) { | ||
return Swap(rules,z,xor2); | ||
} else { | ||
cin = carry; | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
string Output(IEnumerable<Rule> rules, string x, string gate, string y) => | ||
rules.SingleOrDefault(rule => | ||
(rule.in1 == x && rule.kind == gate && rule.in2 == y) || | ||
(rule.in1 == y && rule.kind == gate && rule.in2 == x) | ||
).output; | ||
|
||
IEnumerable<string> Swap(List<Rule> rules, string out1, string out2) { | ||
rules = rules.Select(rule => | ||
rule.output == out1 ? rule with {output = out2} : | ||
rule.output == out2 ? rule with {output = out1} : | ||
rule | ||
).ToList(); | ||
|
||
return Fix(rules).Concat([out1, out2]); | ||
} | ||
|
||
List<Rule> ParseRules(string input) => input | ||
.Split("\n") | ||
.Select(line => { | ||
var parts = line.Split(" "); | ||
return new Rule(in1: parts[0], in2: parts[2], kind: parts[1], output: parts[4]); | ||
}) | ||
.ToList(); | ||
Dictionary<string, Gate> Parse(string input) { | ||
|
||
var res = new Dictionary<string, Gate>(); | ||
|
||
var blocks = input.Split("\n\n"); | ||
|
||
foreach (var line in blocks[0].Split("\n")) { | ||
var parts = line.Split(": "); | ||
res.Add(parts[0], () => int.Parse(parts[1])); | ||
} | ||
|
||
foreach (var line in blocks[1].Split("\n")) { | ||
var parts = Regex.Matches(line, "[a-zA-z0-9]+").Select(m => m.Value).ToArray(); | ||
Gate gate = (parts[0], parts[1], parts[2]) switch { | ||
(var in1, "AND", var in2) => () => res[in1]() & res[in2](), | ||
(var in1, "OR", var in2) => () => res[in1]() | res[in2](), | ||
(var in1, "XOR", var in2) => () => res[in1]() ^ res[in2](), | ||
_ => throw new Exception(), | ||
}; | ||
res.Add(parts[3], gate); | ||
} | ||
return res; | ||
} | ||
|
||
delegate int Gate(); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
61495910098126 | ||
css,cwt,gdd,jmv,pqt,z05,z09,z37 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.