From fbb941dd89dd1b655fe5553a831fad59e86088e4 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Tue, 10 Dec 2019 20:11:50 -0500 Subject: [PATCH] Day 9 --- Advent.csproj.DotSettings | 3 +- Day7/Day7.cs | 10 ++-- Day9/Day9.cs | 20 +++++++ IntcodeComputer.cs | 110 +++++++++++++++++++++++++++----------- Program.cs | 3 +- 5 files changed, 107 insertions(+), 39 deletions(-) create mode 100644 Day9/Day9.cs diff --git a/Advent.csproj.DotSettings b/Advent.csproj.DotSettings index ba3ada0..1e6fa81 100644 --- a/Advent.csproj.DotSettings +++ b/Advent.csproj.DotSettings @@ -6,4 +6,5 @@ True True True - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/Day7/Day7.cs b/Day7/Day7.cs index ee84a30..7f584ee 100644 --- a/Day7/Day7.cs +++ b/Day7/Day7.cs @@ -14,11 +14,11 @@ namespace Advent //var program = "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10"; //var phaseList = new List { "9,7,8,5,6".Split(',').Select(c => int.Parse(c.ToString())).ToArray() }; - int? max = 0; + long? max = 0; foreach (var phases in phaseList) { - var value = 0; + var value = 0L; var amp1 = new IntcodeComputer(program, phases[0]); var amp2 = new IntcodeComputer(program, phases[1]); @@ -42,15 +42,15 @@ namespace Advent Console.WriteLine(max); } - private static IEnumerable GetPhaseList() + private static IEnumerable GetPhaseList() { // This is a stupid brute force way to do it but I'm lazy and it is fast so good enough - var phaseList = new List(); + var phaseList = new List(); for (var i = 0; i <= 99999; i++) { - var phases = i.ToString("00000").ToCharArray().Select(c => int.Parse(c.ToString())).ToArray(); + var phases = i.ToString("00000").ToCharArray().Select(c => long.Parse(c.ToString())).ToArray(); if (phases.All(p => p >= 5) && phases.Distinct().Count() == 5) phaseList.Add(phases); diff --git a/Day9/Day9.cs b/Day9/Day9.cs new file mode 100644 index 0000000..cef0e57 --- /dev/null +++ b/Day9/Day9.cs @@ -0,0 +1,20 @@ +using System; + +namespace Advent +{ + public static class Day9 + { + public static void Execute() + { + var boost = new IntcodeComputer("1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,396,1029,1101,0,356,1023,1101,401,0,1028,1101,24,0,1008,1101,33,0,1019,1101,35,0,1010,1102,359,1,1022,1102,32,1,1001,1101,37,0,1004,1101,0,31,1009,1101,0,30,1003,1101,28,0,1002,1102,1,36,1014,1102,20,1,1012,1101,21,0,1000,1101,0,22,1015,1102,23,1,1013,1102,1,1,1021,1102,1,39,1007,1102,26,1,1017,1101,0,38,1016,1101,0,437,1024,1102,432,1,1025,1101,0,421,1026,1101,0,29,1005,1101,27,0,1011,1102,1,0,1020,1101,0,25,1018,1101,0,414,1027,1102,34,1,1006,109,6,2108,33,-3,63,1005,63,201,1001,64,1,64,1105,1,203,4,187,1002,64,2,64,109,14,21108,40,40,-6,1005,1014,221,4,209,1105,1,225,1001,64,1,64,1002,64,2,64,109,-21,2102,1,3,63,1008,63,28,63,1005,63,251,4,231,1001,64,1,64,1106,0,251,1002,64,2,64,109,12,2101,0,-3,63,1008,63,21,63,1005,63,275,1001,64,1,64,1105,1,277,4,257,1002,64,2,64,109,-10,1207,1,27,63,1005,63,293,1105,1,299,4,283,1001,64,1,64,1002,64,2,64,109,9,21108,41,42,3,1005,1013,315,1105,1,321,4,305,1001,64,1,64,1002,64,2,64,109,-12,1202,6,1,63,1008,63,37,63,1005,63,347,4,327,1001,64,1,64,1105,1,347,1002,64,2,64,109,29,2105,1,-4,1105,1,365,4,353,1001,64,1,64,1002,64,2,64,109,-17,2108,32,-9,63,1005,63,387,4,371,1001,64,1,64,1105,1,387,1002,64,2,64,109,17,2106,0,1,4,393,1105,1,405,1001,64,1,64,1002,64,2,64,109,1,2106,0,-1,1001,64,1,64,1106,0,423,4,411,1002,64,2,64,109,-13,2105,1,9,4,429,1106,0,441,1001,64,1,64,1002,64,2,64,109,3,21107,42,41,-1,1005,1017,461,1001,64,1,64,1106,0,463,4,447,1002,64,2,64,109,-4,21107,43,44,1,1005,1015,481,4,469,1106,0,485,1001,64,1,64,1002,64,2,64,109,-6,21101,44,0,6,1008,1014,47,63,1005,63,505,1106,0,511,4,491,1001,64,1,64,1002,64,2,64,109,-6,1208,-1,32,63,1005,63,529,4,517,1105,1,533,1001,64,1,64,1002,64,2,64,109,11,1205,7,545,1106,0,551,4,539,1001,64,1,64,1002,64,2,64,109,11,21102,45,1,-7,1008,1017,48,63,1005,63,575,1001,64,1,64,1106,0,577,4,557,1002,64,2,64,109,-8,1206,5,593,1001,64,1,64,1105,1,595,4,583,1002,64,2,64,109,7,1206,-3,609,4,601,1106,0,613,1001,64,1,64,1002,64,2,64,109,-10,2101,0,-6,63,1008,63,39,63,1005,63,635,4,619,1106,0,639,1001,64,1,64,1002,64,2,64,109,-9,1208,0,39,63,1005,63,655,1106,0,661,4,645,1001,64,1,64,1002,64,2,64,109,4,2107,25,0,63,1005,63,681,1001,64,1,64,1105,1,683,4,667,1002,64,2,64,109,-5,2107,31,-2,63,1005,63,701,4,689,1106,0,705,1001,64,1,64,1002,64,2,64,109,19,1205,-1,719,4,711,1105,1,723,1001,64,1,64,1002,64,2,64,109,-17,1201,3,0,63,1008,63,24,63,1005,63,745,4,729,1106,0,749,1001,64,1,64,1002,64,2,64,109,13,21102,46,1,-3,1008,1015,46,63,1005,63,771,4,755,1105,1,775,1001,64,1,64,1002,64,2,64,109,-13,1207,4,32,63,1005,63,793,4,781,1106,0,797,1001,64,1,64,1002,64,2,64,109,7,2102,1,-9,63,1008,63,27,63,1005,63,821,1001,64,1,64,1105,1,823,4,803,1002,64,2,64,109,-18,1201,8,0,63,1008,63,25,63,1005,63,847,1001,64,1,64,1106,0,849,4,829,1002,64,2,64,109,23,21101,47,0,2,1008,1019,47,63,1005,63,871,4,855,1106,0,875,1001,64,1,64,1002,64,2,64,109,-22,1202,5,1,63,1008,63,19,63,1005,63,899,1001,64,1,64,1106,0,901,4,881,4,64,99,21102,27,1,1,21102,1,915,0,1105,1,922,21201,1,25165,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21102,942,1,0,1105,1,922,22102,1,1,-1,21201,-2,-3,1,21101,0,957,0,1105,1,922,22201,1,-1,-2,1106,0,968,21201,-2,0,-2,109,-3,2105,1,0", 2); + + while (!boost.Halted) + { + var value = boost.Execute(0); + + if (!boost.Halted) + Console.WriteLine(value); + } + } + } +} diff --git a/IntcodeComputer.cs b/IntcodeComputer.cs index 8292cf3..7380afa 100644 --- a/IntcodeComputer.cs +++ b/IntcodeComputer.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; namespace Advent @@ -9,30 +8,60 @@ namespace Advent private enum ParameterMode { Position, - Immediate + Immediate, + Relative } - private readonly int[] _memory; - private readonly int _phase; + private readonly long[] _memory; + private readonly long _phase; private bool _setPhase; - private int _instructionPointer; - private int _output; + private long _instructionPointer; + private long _output; + private long _relativeBase; public bool Halted { get; private set; } - public IntcodeComputer(string memoryString, int phase) + public IntcodeComputer(string memoryString, long phase) { - _memory = memoryString.Split(',').Select(int.Parse).ToArray(); + _memory = new long[5000]; + + memoryString.Split(',').Select(long.Parse).ToArray().CopyTo(_memory, 0); + _phase = phase; } - private static int GetValue(IReadOnlyList codes, int parameter, ParameterMode mode) + private long GetValue(long parameter, ParameterMode mode) { - return mode == ParameterMode.Immediate ? parameter : codes[parameter]; + switch (mode) + { + case ParameterMode.Position: + return _memory[parameter]; + case ParameterMode.Immediate: + return parameter; + case ParameterMode.Relative: + return _memory[_relativeBase + parameter]; + default: + throw new ArgumentOutOfRangeException(nameof(mode), mode, null); + } } - public int Execute(int input) + private void SetValue(long value, long location, ParameterMode mode) + { + switch (mode) + { + case ParameterMode.Position: + _memory[location] = value; + break; + case ParameterMode.Relative: + _memory[_relativeBase + location] = value; + break; + default: + throw new ArgumentOutOfRangeException(nameof(mode), mode, null); + } + } + + public long Execute(long input) { while (!Halted) { @@ -41,32 +70,42 @@ namespace Advent switch (opCode) { case 1: - _memory[_memory[_instructionPointer + 3]] = GetValue(_memory, _memory[_instructionPointer + 1], mode1) + GetValue(_memory, _memory[_instructionPointer + 2], mode2); + { + var newValue = GetValue(_memory[_instructionPointer + 1], mode1) + GetValue(_memory[_instructionPointer + 2], mode2); - _instructionPointer += 4; + SetValue(newValue, _memory[_instructionPointer + 3], mode3); - break; + _instructionPointer += 4; + + break; + } case 2: - _memory[_memory[_instructionPointer + 3]] = GetValue(_memory, _memory[_instructionPointer + 1], mode1) * GetValue(_memory, _memory[_instructionPointer + 2], mode2); + { + var newValue = GetValue(_memory[_instructionPointer + 1], mode1) * GetValue(_memory[_instructionPointer + 2], mode2); - _instructionPointer += 4; + SetValue(newValue, _memory[_instructionPointer + 3], mode3); - break; + _instructionPointer += 4; + + break; + } case 3: var inputValue = _setPhase ? input : _phase; _setPhase = true; - _memory[_memory[_instructionPointer + 1]] = inputValue; + var writeValue = _memory[_instructionPointer + 1]; + + SetValue(inputValue, _memory[_instructionPointer + 3], mode3); _instructionPointer += 2; break; case 4: - _output = GetValue(_memory, _memory[_instructionPointer + 1], ParameterMode.Position); + _output = GetValue(_memory[_instructionPointer + 1], mode1); _instructionPointer += 2; @@ -74,10 +113,10 @@ namespace Advent case 5: { - var value = GetValue(_memory, _memory[_instructionPointer + 1], mode1); + var value = GetValue(_memory[_instructionPointer + 1], mode1); if (value != 0) - _instructionPointer = GetValue(_memory, _memory[_instructionPointer + 2], mode2); + _instructionPointer = GetValue(_memory[_instructionPointer + 2], mode2); else _instructionPointer += 3; } @@ -86,10 +125,10 @@ namespace Advent case 6: { - var value = GetValue(_memory, _memory[_instructionPointer + 1], mode1); + var value = GetValue(_memory[_instructionPointer + 1], mode1); if (value == 0) - _instructionPointer = GetValue(_memory, _memory[_instructionPointer + 2], mode2); + _instructionPointer = GetValue(_memory[_instructionPointer + 2], mode2); else _instructionPointer += 3; } @@ -98,13 +137,13 @@ namespace Advent case 7: { - var value1 = GetValue(_memory, _memory[_instructionPointer + 1], mode1); - var value2 = GetValue(_memory, _memory[_instructionPointer + 2], mode2); + var value1 = GetValue(_memory[_instructionPointer + 1], mode1); + var value2 = GetValue(_memory[_instructionPointer + 2], mode2); if (value1 < value2) - _memory[_memory[_instructionPointer + 3]] = 1; + SetValue(1, _memory[_instructionPointer + 3], mode3); else - _memory[_memory[_instructionPointer + 3]] = 0; + SetValue(0, _memory[_instructionPointer + 3], mode3); _instructionPointer += 4; } @@ -113,19 +152,26 @@ namespace Advent case 8: { - var value1 = GetValue(_memory, _memory[_instructionPointer + 1], mode1); - var value2 = GetValue(_memory, _memory[_instructionPointer + 2], mode2); + var value1 = GetValue(_memory[_instructionPointer + 1], mode1); + var value2 = GetValue(_memory[_instructionPointer + 2], mode2); if (value1 == value2) - _memory[_memory[_instructionPointer + 3]] = 1; + SetValue(1, _memory[_instructionPointer + 3], mode3); else - _memory[_memory[_instructionPointer + 3]] = 0; + SetValue(0, _memory[_instructionPointer + 3], mode3); _instructionPointer += 4; } break; + case 9: + _relativeBase += GetValue(_memory[_instructionPointer + 1], mode1); + + _instructionPointer += 2; + + break; + case 99: Halted = true; @@ -136,7 +182,7 @@ namespace Advent return _output; } - private static (ParameterMode mode1, ParameterMode mode2, ParameterMode mode3, int opCode) ParseOpCode(int fullOpCode) + private static (ParameterMode mode1, ParameterMode mode2, ParameterMode mode3, int opCode) ParseOpCode(long fullOpCode) { var codeString = fullOpCode.ToString("00000").ToCharArray(); diff --git a/Program.cs b/Program.cs index 2fac65d..3f7b452 100644 --- a/Program.cs +++ b/Program.cs @@ -11,7 +11,8 @@ //Day5.Execute(); //Day6.Execute(); //Day7.Execute(); - Day8.Execute(); + //Day8.Execute(); + Day9.Execute(); } } }