From c48d5e99055e82ee3e6ed01730e14ca79f2ec275 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 11 Dec 2019 23:47:39 -0500 Subject: [PATCH] day 11 --- Day11.cs | 80 +++++++++++++++++++++++++++++++------------------- aoc2019.csproj | 3 ++ aoc2019.sln | 25 ++++++++++++++++ 3 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 aoc2019.sln diff --git a/Day11.cs b/Day11.cs index 1c1e270..6ec157d 100644 --- a/Day11.cs +++ b/Day11.cs @@ -1,8 +1,9 @@ using aoc2019.lib; using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; namespace aoc2019 { @@ -10,17 +11,13 @@ namespace aoc2019 { public override int DayNumber => 11; - private IntCodeVM vm; - List> paintmap; - long x, y; - Direction heading; + private readonly IntCodeVM vm; + private long x, y; + private Direction heading; public Day11() { vm = new IntCodeVM(Input.First()); - paintmap = new List>(); - x = 0; y = 0; - heading = Direction.Up; } enum Direction @@ -28,14 +25,14 @@ namespace aoc2019 Up, Down, Left, Right } - private (long, long) DxDy() + private void Move() { - return heading switch + switch (heading) { - Direction.Up => (0, 1), - Direction.Down => (0, -1), - Direction.Left => (-1, 0), - Direction.Right => (1, 0) + case Direction.Up: y++; break; + case Direction.Down: y--; break; + case Direction.Left: x--; break; + case Direction.Right: x++; break; }; } @@ -43,33 +40,54 @@ namespace aoc2019 { switch (heading) { - case Direction.Up: heading = direction == 0 ? Direction.Left : Direction.Right; break; - case Direction.Down: heading = direction == 0 ? Direction.Right : Direction.Left; break; - case Direction.Left: heading = direction == 0 ? Direction.Down : Direction.Up; break; + case Direction.Up: heading = direction == 0 ? Direction.Left : Direction.Right; break; + case Direction.Down: heading = direction == 0 ? Direction.Right : Direction.Left; break; + case Direction.Left: heading = direction == 0 ? Direction.Down : Direction.Up; break; case Direction.Right: heading = direction == 0 ? Direction.Up : Direction.Down; break; } + Move(); + } + + private Dictionary<(long x, long y), long> PaintShip(int initialVal) + { + var map = new Dictionary<(long, long), long>(); + vm.Reset(); + heading = Direction.Up; + x = 0; y = 0; + map[(x, y)] = initialVal; + + var haltType = IntCodeVM.HaltType.Waiting; + while (haltType == IntCodeVM.HaltType.Waiting) + { + haltType = vm.Run(map.GetValueOrDefault((x, y))); + map[(x, y)] = vm.Result; + Turn(vm.Result); + } + + return map; } public override string Part1() { - vm.Reset(); - vm.Run(); - var output = vm.output.ToList(); - long dx, dy; - for (var i = 0; i < output.Count; i += 2) - { - long color = output[i]; - Turn(output[i + 1]); - paintmap[x][y] = color == 0; - (dx, dy) = DxDy(); - x += dx; y += dy; - } - return $"{paintmap.Count(x => x != null)}"; + return $"{PaintShip(0).Count}"; } public override string Part2() { - return ""; + var map = PaintShip(1); + int minX = (int)map.Keys.Select(x => x.x).Min(); + int maxX = (int)map.Keys.Select(x => x.x).Max(); + int minY = (int)map.Keys.Select(x => x.y).Min(); + int maxY = (int)map.Keys.Select(x => x.y).Max(); + + return Enumerable.Range(minY, maxY - minY + 1) + .Select(y => + Enumerable.Range(minX, maxX - minX + 1) + .Select(x => map.GetValueOrDefault((x, y)) == 0 ? ' ' : '#') + .ToDelimitedString() + ) + .Reverse() + .ToDelimitedString(Environment.NewLine); } } } diff --git a/aoc2019.csproj b/aoc2019.csproj index eca0950..8a9bb28 100644 --- a/aoc2019.csproj +++ b/aoc2019.csproj @@ -9,6 +9,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/aoc2019.sln b/aoc2019.sln new file mode 100644 index 0000000..fe311d7 --- /dev/null +++ b/aoc2019.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.181 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aoc2019", "aoc2019.csproj", "{0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BBA89C41-4703-4C98-A8EC-54957C0E673E} + EndGlobalSection +EndGlobal