Skip to content

Commit

Permalink
Merge pull request #18 from MartinZikmund/feature/aoc2024-day17
Browse files Browse the repository at this point in the history
AoC 2024 Day 17 Solutions
  • Loading branch information
MartinZikmund authored Dec 20, 2024
2 parents b96b743 + 9896db1 commit b941e03
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/AdventOfCode.Puzzles/2024/17/Part1/Part1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace AdventOfCode.Puzzles._2024._17.Part1;

public partial class Part1 : IPuzzleSolution
{
public async Task<string> SolveAsync(StreamReader inputReader)
{
return Execute(47792830, 0, 0);
}

private string Execute(long a, long b, long c)
{
StringBuilder output = new();

do
{
b = a % 8;
b = b ^ 5;
c = a / (long)Math.Pow(2, b);
b = b ^ 6;
b = b ^ c;
output.Append((b % 8).ToString() + ",");
a = a / 8;
} while (a != 0);

return output.Remove(output.Length - 1, 1).ToString();
}
}
58 changes: 58 additions & 0 deletions src/AdventOfCode.Puzzles/2024/17/Part2/Part2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Xml;

namespace AdventOfCode.Puzzles._2024._17.Part2;

public partial class Part2 : IPuzzleSolution
{
private ulong _best = ulong.MaxValue;
private string _expectedOutput = "2415751643550330";

public async Task<string> SolveAsync(StreamReader inputReader)
{
// 2,4,1,5,7,5,1,6,4,3,5,5,0,3,3,0

// A0 = 0-7
// A1 = A0 * 8 - A0 * 8 + 7

Solve(0UL, _expectedOutput.Length - 1);
var output = Execute(107416732707226);
return _best.ToString();
}

private void Solve(ulong currentA, int index)
{
if (index == -1)
{
_best = Math.Min(_best, currentA);
return;
}

var next = _expectedOutput[index];
for (int remainder = 0; remainder < 8; remainder++)
{
var nextA = currentA * 8 + (ulong)remainder;
var result = Execute(nextA);
if (_expectedOutput.EndsWith(result))
{
Solve(nextA, index - 1);
}
}
}

private string Execute(ulong a)
{
StringBuilder output = new();
do
{
ulong b = a % 8;
b = b ^ 5;
ulong c = a / (ulong)Math.Pow(2, b);
b = b ^ 6;
b = b ^ c;
output.Append((b % 8).ToString());
a = a / 8;
} while (a != 0);

return output.ToString();
}
}
17 changes: 17 additions & 0 deletions src/AdventOfCode.Puzzles/2024/17/TestData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################

0 comments on commit b941e03

Please sign in to comment.