feat: add day 17 part 2 puzzle solution...but really only works on small input
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<int, Func<int, bool>> _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<int> _output = [];
|
||||
private readonly List<long> _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,
|
||||
|
||||
Reference in New Issue
Block a user