From 98f3016b477077a8478895b67ee37af71ae68717 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 16 Dec 2020 20:10:05 -0500 Subject: [PATCH] start on day 19 --- aoc2019/README.md => README.md | 0 aoc2019.test/Tests.cs | 1 + aoc2019/Day19.cs | 35 ++++++++++++++++++++++++++++++++++ aoc2019/lib/IntCodeVM.cs | 12 ++++++------ 4 files changed, 42 insertions(+), 6 deletions(-) rename aoc2019/README.md => README.md (100%) create mode 100644 aoc2019/Day19.cs diff --git a/aoc2019/README.md b/README.md similarity index 100% rename from aoc2019/README.md rename to README.md diff --git a/aoc2019.test/Tests.cs b/aoc2019.test/Tests.cs index 8e8d0f9..61f9093 100644 --- a/aoc2019.test/Tests.cs +++ b/aoc2019.test/Tests.cs @@ -26,6 +26,7 @@ namespace aoc2019.test [DataRow(typeof(Day15), "280", "400")] [DataRow(typeof(Day16), "90744714", "82994322")] [DataRow(typeof(Day17), "2804", "")] + [DataRow(typeof(Day19), "", "")] public void TestAllDays(Type dayType, string part1, string part2) { // create day instance diff --git a/aoc2019/Day19.cs b/aoc2019/Day19.cs new file mode 100644 index 0000000..73d3e22 --- /dev/null +++ b/aoc2019/Day19.cs @@ -0,0 +1,35 @@ +using System.Linq; +using aoc2019.lib; + +namespace aoc2019 +{ + public sealed class Day19 : Day + { + private readonly IntCodeVM vm; + + public Day19() : base(19, "Tractor Beam") + { + vm = new IntCodeVM(Input.First()); + } + + public override string Part1() + { + var count = 0; + + for (var x = 0; x < 50; x++) + for (var y = 0; y < 50; y++) + { + vm.AddInput(x, y); + vm.Run(); + if (vm.Result == 1) count++; + } + + return $"{count}"; + } + + public override string Part2() + { + return ""; + } + } +} diff --git a/aoc2019/lib/IntCodeVM.cs b/aoc2019/lib/IntCodeVM.cs index 2190579..4fb196f 100644 --- a/aoc2019/lib/IntCodeVM.cs +++ b/aoc2019/lib/IntCodeVM.cs @@ -16,12 +16,12 @@ namespace aoc2019.lib private long i; public Queue input, output; public long[] memory; - private long relbase; + private long relativeBase; public IntCodeVM(string tape) { i = 0; - relbase = 0; + relativeBase = 0; program = tape.Split(',').Select(long.Parse).ToArray(); memory = program; input = new Queue(); @@ -33,7 +33,7 @@ namespace aoc2019.lib public void Reset() { i = 0; - relbase = 0; + relativeBase = 0; memory = program; input.Clear(); output.Clear(); @@ -77,7 +77,7 @@ namespace aoc2019.lib { case 0: return MemGet(param); case 1: return param; - case 2: return MemGet(relbase + param); + case 2: return MemGet(relativeBase + param); default: throw new Exception("invalid parameter mode"); } } @@ -92,7 +92,7 @@ namespace aoc2019.lib break; case 1: throw new Exception("cannot set in immediate mode"); case 2: - MemSet(relbase + param, val); + MemSet(relativeBase + param, val); break; default: throw new Exception("invalid parameter mode"); } @@ -144,7 +144,7 @@ namespace aoc2019.lib i += 4; break; case 9: - relbase += Get(1); + relativeBase += Get(1); i += 2; break; case 99: