Skip to content

Commit 991efdf

Browse files
Merge pull request #25 from MartinZikmund/feature/aoc2024-day24
AoC 2024 Day 24 Solutions
2 parents a8294f1 + 3c54aa5 commit 991efdf

File tree

5 files changed

+455
-0
lines changed

5 files changed

+455
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace AdventOfCode.Puzzles._2024._24;
2+
3+
public class Connection
4+
{
5+
private Connection(string input1, string input2, string output, Operation operation)
6+
{
7+
Input1 = input1;
8+
Input2 = input2;
9+
Output = output;
10+
Operation = operation;
11+
}
12+
13+
public string Input1 { get; }
14+
15+
public string Input2 { get; }
16+
17+
public string Output { get; private set; }
18+
19+
public Operation Operation { get; }
20+
21+
public bool CanEvaluate(Dictionary<string, int> knownGates) =>
22+
knownGates.ContainsKey(Input1) && knownGates.ContainsKey(Input2);
23+
24+
public void Evaluate(Dictionary<string, int> knownGates)
25+
{
26+
if (!CanEvaluate(knownGates))
27+
{
28+
throw new InvalidOperationException("Gate inputs not known.");
29+
}
30+
31+
var input1 = knownGates[Input1];
32+
var input2 = knownGates[Input2];
33+
var output = Operation switch
34+
{
35+
Operation.And => input1 & input2,
36+
Operation.Or => input1 | input2,
37+
Operation.Xor => input1 ^ input2,
38+
_ => throw new InvalidOperationException()
39+
};
40+
41+
knownGates[Output] = output;
42+
}
43+
44+
public static Connection ParseConnection(string line)
45+
{
46+
var parts = line.Split(" ");
47+
var input1 = parts[0];
48+
var input2 = parts[2];
49+
var output = parts[4];
50+
var operation = parts[1] switch
51+
{
52+
"AND" => Operation.And,
53+
"OR" => Operation.Or,
54+
"XOR" => Operation.Xor,
55+
_ => throw new InvalidOperationException()
56+
};
57+
58+
return new Connection(input1, input2, output, operation);
59+
}
60+
61+
public void SwapOutput(Connection otherConnection)
62+
{
63+
var temp = Output;
64+
Output = otherConnection.Output;
65+
otherConnection.Output = temp;
66+
}
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AdventOfCode.Puzzles._2024._24;
8+
9+
public enum Operation
10+
{
11+
And,
12+
Or,
13+
Xor
14+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace AdventOfCode.Puzzles._2024._24.Part1;
2+
3+
public partial class Part1 : IPuzzleSolution
4+
{
5+
private List<Connection> _connections = new();
6+
private HashSet<string> _gates = new();
7+
8+
public async Task<string> SolveAsync(StreamReader inputReader)
9+
{
10+
var inputValues = new Dictionary<string, int>();
11+
while (await inputReader.ReadLineAsync() is { } line && !string.IsNullOrEmpty(line))
12+
{
13+
var parts = line.Split(": ");
14+
var gateName = parts[0];
15+
var value = int.Parse(parts[1]);
16+
inputValues.Add(gateName, value);
17+
_gates.Add(gateName);
18+
}
19+
20+
// Read connections
21+
while (await inputReader.ReadLineAsync() is { } line)
22+
{
23+
var connection = Connection.ParseConnection(line);
24+
25+
_gates.Add(connection.Input1);
26+
_gates.Add(connection.Input2);
27+
_gates.Add(connection.Output);
28+
_connections.Add(connection);
29+
}
30+
31+
var zGates = _gates.Where(g => g.StartsWith("z")).OrderBy(g => g).Reverse().ToList();
32+
33+
Evaluate(inputValues);
34+
35+
var result = GetZValue(zGates, inputValues);
36+
return result.ToString();
37+
}
38+
39+
private long GetZValue(List<string> zGates, Dictionary<string, int> values)
40+
{
41+
var binary = string.Join("", zGates.Select(g => values[g]));
42+
return Convert.ToInt64(binary, 2);
43+
}
44+
45+
private void Evaluate(Dictionary<string, int> values)
46+
{
47+
var gatesToProcess = new Queue<string>();
48+
var processedConnections = new HashSet<Connection>();
49+
while (values.Count < _gates.Count)
50+
{
51+
foreach (var connection in _connections)
52+
{
53+
if (!processedConnections.Contains(connection) &&
54+
connection.CanEvaluate(values))
55+
{
56+
connection.Evaluate(values);
57+
processedConnections.Add(connection);
58+
}
59+
}
60+
}
61+
62+
}
63+
}

0 commit comments

Comments
 (0)