From e799a64b352c2899e0a56c793823343a15d2430f Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 19 Dec 2024 01:29:08 -0600 Subject: [PATCH] feat: add day 17 part 2 puzzle solution...but really only works on small input --- .../ComputerTests.cs | 15 ++++++-- 17/ChronospatialComputer/Program.cs | 35 ++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/17/ChronospatialComputer.Tests/ComputerTests.cs b/17/ChronospatialComputer.Tests/ComputerTests.cs index 6b3b9f9..c1cdaca 100644 --- a/17/ChronospatialComputer.Tests/ComputerTests.cs +++ b/17/ChronospatialComputer.Tests/ComputerTests.cs @@ -2,13 +2,22 @@ public class ComputerTests { - private readonly string _exampleInput = $"Register A: 729{Environment.NewLine}Register B: 0{Environment.NewLine}Register C: 0{Environment.NewLine}{Environment.NewLine}Program: 0,1,5,4,3,0"; - + private readonly string _runProgramExampleInput = $"Register A: 729{Environment.NewLine}Register B: 0{Environment.NewLine}Register C: 0{Environment.NewLine}{Environment.NewLine}Program: 0,1,5,4,3,0"; + private readonly string _findCopyExampleInput = $"Register A: 2024{Environment.NewLine}Register B: 0{Environment.NewLine}Register C: 0{Environment.NewLine}{Environment.NewLine}Program: 0,3,5,4,3,0"; + [Test] public async Task RunProgram_WhenCalledWithExampleInput_ItShouldReturnExpectedValue() { - var result = Computer.From(_exampleInput).RunProgram(); + var result = Computer.From(_runProgramExampleInput).RunProgram(); await Assert.That(result).IsEqualTo("4,6,3,5,6,3,5,2,1,0"); } + + [Test] + public async Task FindCopy_WhenCalledWithExampleInput_ItShouldReturnExpectedValue() + { + var result = Computer.From(_findCopyExampleInput).FindCopy(); + + await Assert.That(result).IsEqualTo(117440); + } } \ No newline at end of file diff --git a/17/ChronospatialComputer/Program.cs b/17/ChronospatialComputer/Program.cs index 2eec298..fb4a301 100644 --- a/17/ChronospatialComputer/Program.cs +++ b/17/ChronospatialComputer/Program.cs @@ -18,23 +18,27 @@ var input = await File.ReadAllTextAsync(args[0]); var stopwatch = new Stopwatch(); stopwatch.Start(); -var result = Computer.From(input).RunProgram(); +var computer = Computer.From(input); + +var msg = isPart2 + ? $"The program outputs a copy at itself when the A register is: {computer.FindCopy()}" + : $"The output of the program is {computer.RunProgram()}"; stopwatch.Stop(); -Console.WriteLine($"The output of the program is {result}. ({stopwatch.ElapsedMilliseconds}ms)"); +Console.WriteLine($"{msg}. ({stopwatch.ElapsedMilliseconds}ms)"); class Computer { private readonly int[] _program; private readonly Dictionary> _instructions; - private int _aRegister; - private int _bRegister; - private int _cRegister; + private long _aRegister; + private long _bRegister; + private long _cRegister; private int _instructionPointer; - private readonly List _output = []; + private readonly List _output = []; - private Computer(int a, int b, int c, int[] program) + private Computer(long a, long b, long c, int[] program) { _aRegister = a; _bRegister = b; @@ -107,7 +111,22 @@ class Computer return string.Join(',', _output); } - private int GetComboOperand(int operand) => operand switch + public long FindCopy() + { + long lowestInitialValue = _aRegister; + var targetOutput = string.Join(',', _program); + string output; + + do + { + lowestInitialValue += 1; + output = new Computer(lowestInitialValue, _bRegister, _cRegister, _program).RunProgram(); + } while (output != targetOutput); + + return lowestInitialValue; + } + + private long GetComboOperand(int operand) => operand switch { 0 or 1 or 2 or 3 => operand, 4 => _aRegister,