Day 23 part 1

This commit is contained in:
2018-12-23 06:48:41 +01:00
parent 6128917e02
commit fe7c9c828c
6 changed files with 1141 additions and 1 deletions

View File

@@ -72,6 +72,7 @@
<Compile Include="Day11_Tests.cs" /> <Compile Include="Day11_Tests.cs" />
<Compile Include="Day12_Tests.cs" /> <Compile Include="Day12_Tests.cs" />
<Compile Include="Day13_Tests.cs" /> <Compile Include="Day13_Tests.cs" />
<Compile Include="Day23_Tests.cs" />
<Compile Include="GuardEvent_Tests.cs" /> <Compile Include="GuardEvent_Tests.cs" />
<Compile Include="MarbleGame_Tests.cs" /> <Compile Include="MarbleGame_Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AdventOfCode2018.Tests
{
[TestClass()]
public class Day23_Tests
{
[TestMethod()]
public void ResolvePart1__Test()
{
Day23 day = new Day23();
string result = day.ResolvePart1(new string[] {
"pos=<0,0,0>, r=4",
"pos=<1,0,0>, r=1",
"pos=<4,0,0>, r=3",
"pos=<0,2,0>, r=1",
"pos=<0,5,0>, r=3",
"pos=<0,0,3>, r=1",
"pos=<1,1,1>, r=1",
"pos=<1,1,2>, r=1",
"pos=<1,3,1>, r=1",
});
Assert.AreEqual("7", result);
}
}
}

View File

@@ -56,6 +56,7 @@
<Compile Include="Day11.cs" /> <Compile Include="Day11.cs" />
<Compile Include="Day12.cs" /> <Compile Include="Day12.cs" />
<Compile Include="Day13.cs" /> <Compile Include="Day13.cs" />
<Compile Include="Day23.cs" />
<Compile Include="IDay.cs" /> <Compile Include="IDay.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@@ -97,6 +98,9 @@
<Content Include="inputs\Day12.txt"> <Content Include="inputs\Day12.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="inputs\Day23.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="inputs\Day13.txt"> <Content Include="inputs\Day13.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>

106
AdventOfCode2018/Day23.cs Normal file
View File

@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2018
{
/*
--- Day 23: Experimental Emergency Teleportation ---
Using your torch to search the darkness of the rocky cavern, you finally locate the man's friend: a small reindeer.
You're not sure how it got so far in this cave. It looks sick - too sick to walk - and too heavy for you to carry all the way back. Sleighs won't be invented for another 1500 years, of course.
The only option is experimental emergency teleportation.
You hit the "experimental emergency teleportation" button on the device and push I accept the risk on no fewer than 18 different warning messages. Immediately, the device deploys hundreds of tiny nanobots which fly around the cavern, apparently assembling themselves into a very specific formation. The device lists the X,Y,Z position (pos) for each nanobot as well as its signal radius (r) on its tiny screen (your puzzle input).
Each nanobot can transmit signals to any integer coordinate which is a distance away from it less than or equal to its signal radius (as measured by Manhattan distance). Coordinates a distance away of less than or equal to a nanobot's signal radius are said to be in range of that nanobot.
Before you start the teleportation process, you should determine which nanobot is the strongest (that is, which has the largest signal radius) and then, for that nanobot, the total number of nanobots that are in range of it, including itself.
For example, given the following nanobots:
pos=<0,0,0>, r=4
pos=<1,0,0>, r=1
pos=<4,0,0>, r=3
pos=<0,2,0>, r=1
pos=<0,5,0>, r=3
pos=<0,0,3>, r=1
pos=<1,1,1>, r=1
pos=<1,1,2>, r=1
pos=<1,3,1>, r=1
The strongest nanobot is the first one (position 0,0,0) because its signal radius, 4 is the largest. Using that nanobot's location and signal radius, the following nanobots are in or out of range:
The nanobot at 0,0,0 is distance 0 away, and so it is in range.
The nanobot at 1,0,0 is distance 1 away, and so it is in range.
The nanobot at 4,0,0 is distance 4 away, and so it is in range.
The nanobot at 0,2,0 is distance 2 away, and so it is in range.
The nanobot at 0,5,0 is distance 5 away, and so it is not in range.
The nanobot at 0,0,3 is distance 3 away, and so it is in range.
The nanobot at 1,1,1 is distance 3 away, and so it is in range.
The nanobot at 1,1,2 is distance 4 away, and so it is in range.
The nanobot at 1,3,1 is distance 5 away, and so it is not in range.
In this example, in total, 7 nanobots are in range of the nanobot with the largest signal radius.
Find the nanobot with the largest signal radius. How many nanobots are in range of its signals?
*/
public class Day23 : IDay
{
public string ResolvePart1(string[] inputs)
{
List<NanoBot> nanoBots = inputs
.Select(strInput => NanoBot.FromString(strInput))
.Where(nanoBot => nanoBot != null)
.ToList();
NanoBot bestNanoBot = nanoBots.OrderBy(nanoBot => nanoBot.Range).LastOrDefault();
int countInRange = nanoBots.Where(nanoBot => bestNanoBot.InRange(nanoBot)).Count();
return countInRange.ToString();
}
public string ResolvePart2(string[] inputs)
{
return null;
}
public class NanoBot
{
public long X { get; set; }
public long Y { get; set; }
public long Z { get; set; }
public long Range { get; set; }
public static NanoBot FromString(string strInput)
{
string[] parts = strInput.Split(new string[] { "pos=<", ",", ">, r=", }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 4) { return null; }
NanoBot nanoBot = new NanoBot
{
X = Convert.ToInt64(parts[0]),
Y = Convert.ToInt64(parts[1]),
Z = Convert.ToInt64(parts[2]),
Range = Convert.ToInt64(parts[3]),
};
return nanoBot;
}
public long ManhattanDistance(NanoBot other)
{
long distance = Math.Abs(X - other.X) + Math.Abs(Y - other.Y) + Math.Abs(Z - other.Z);
return distance;
}
public bool InRange(NanoBot other)
{
long distance = ManhattanDistance(other);
return distance <= Range;
}
}
}
}

View File

@@ -7,7 +7,7 @@ namespace AdventOfCode2018
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
int currentDayNumber = 13; int currentDayNumber = 23;
IDay currentDay = null; IDay currentDay = null;
switch (currentDayNumber) switch (currentDayNumber)
@@ -25,6 +25,7 @@ namespace AdventOfCode2018
case 11: currentDay = new Day11(); break; case 11: currentDay = new Day11(); break;
case 12: currentDay = new Day12(); break; case 12: currentDay = new Day12(); break;
case 13: currentDay = new Day13(); break; case 13: currentDay = new Day13(); break;
case 23: currentDay = new Day23(); break;
} }
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber)); string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));

File diff suppressed because it is too large Load Diff