day 11
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
ddb972d58f
commit
c48d5e9905
80
Day11.cs
80
Day11.cs
|
@ -1,8 +1,9 @@
|
||||||
using aoc2019.lib;
|
using aoc2019.lib;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace aoc2019
|
namespace aoc2019
|
||||||
{
|
{
|
||||||
|
@ -10,17 +11,13 @@ namespace aoc2019
|
||||||
{
|
{
|
||||||
public override int DayNumber => 11;
|
public override int DayNumber => 11;
|
||||||
|
|
||||||
private IntCodeVM vm;
|
private readonly IntCodeVM vm;
|
||||||
List<List<bool>> paintmap;
|
private long x, y;
|
||||||
long x, y;
|
private Direction heading;
|
||||||
Direction heading;
|
|
||||||
|
|
||||||
public Day11()
|
public Day11()
|
||||||
{
|
{
|
||||||
vm = new IntCodeVM(Input.First());
|
vm = new IntCodeVM(Input.First());
|
||||||
paintmap = new List<List<bool>>();
|
|
||||||
x = 0; y = 0;
|
|
||||||
heading = Direction.Up;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Direction
|
enum Direction
|
||||||
|
@ -28,14 +25,14 @@ namespace aoc2019
|
||||||
Up, Down, Left, Right
|
Up, Down, Left, Right
|
||||||
}
|
}
|
||||||
|
|
||||||
private (long, long) DxDy()
|
private void Move()
|
||||||
{
|
{
|
||||||
return heading switch
|
switch (heading)
|
||||||
{
|
{
|
||||||
Direction.Up => (0, 1),
|
case Direction.Up: y++; break;
|
||||||
Direction.Down => (0, -1),
|
case Direction.Down: y--; break;
|
||||||
Direction.Left => (-1, 0),
|
case Direction.Left: x--; break;
|
||||||
Direction.Right => (1, 0)
|
case Direction.Right: x++; break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,33 +40,54 @@ namespace aoc2019
|
||||||
{
|
{
|
||||||
switch (heading)
|
switch (heading)
|
||||||
{
|
{
|
||||||
case Direction.Up: heading = direction == 0 ? Direction.Left : Direction.Right; 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.Down: heading = direction == 0 ? Direction.Right : Direction.Left; break;
|
||||||
case Direction.Left: heading = direction == 0 ? Direction.Down : Direction.Up; break;
|
case Direction.Left: heading = direction == 0 ? Direction.Down : Direction.Up; break;
|
||||||
case Direction.Right: heading = direction == 0 ? Direction.Up : Direction.Down; 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()
|
public override string Part1()
|
||||||
{
|
{
|
||||||
vm.Reset();
|
return $"{PaintShip(0).Count}";
|
||||||
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)}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part2()
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,9 @@
|
||||||
<None Update="input\day1.in">
|
<None Update="input\day1.in">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="input\day11.in">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="input\day2.in">
|
<None Update="input\day2.in">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue