Skip to content

Commit

Permalink
Day 24 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 25, 2024
1 parent d97ea22 commit f2f05cb
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions AdventOfCode/Day24.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,35 @@ public override ValueTask<string> Solve_1()
CalculateValueForOutput(output, rulesByOutput, values);
}

string[] outputs = rulesByOutput.Keys.Where(k => k.StartsWith("z")).OrderBy(k => k).ToArray();

BitArray bitArray = new BitArray(outputs.Select(o => values[o]).ToArray());

long res = GetIntFromBitArray(bitArray);
long res = GetInteger("z", rulesByOutput, values);

return new(res.ToString());
}

public override ValueTask<string> Solve_2()
{
(Dictionary<string, bool> values, List<Rule> rules) = Init();

Dictionary<string, Rule> rulesByOutput = ToDicitonary(rules);

foreach (string output in rulesByOutput.Keys)
{
CalculateValueForOutput(output, rulesByOutput, values);
}

long x = GetInteger("x", rulesByOutput, values);
long y = GetInteger("y", rulesByOutput, values);
long z = GetInteger("z", rulesByOutput, values);

if (x + y == z)
{
Console.WriteLine($"{x} + {y} = {z}");
}
else
{
Console.WriteLine($"{x} + {y} != {z}");
}

return new("");
}

Expand Down Expand Up @@ -108,6 +126,16 @@ private void CalculateValueForOutput(string output, Dictionary<string, Rule> rul
}
}

private long GetInteger(string letter, Dictionary<string, Rule> rulesByOutput, Dictionary<string, bool> values)
{
string[] outputs = values.Keys.Where(k => k.StartsWith(letter)).OrderBy(k => k).ToArray();

BitArray bitArray = new BitArray(outputs.Select(o => values[o]).ToArray());

long res = GetIntFromBitArray(bitArray);
return res;
}

private long GetIntFromBitArray(BitArray bitArray)
{
var array = new byte[8];
Expand Down

0 comments on commit f2f05cb

Please sign in to comment.