Completed Day 9 p 2

This commit is contained in:
Rob Stoffelen 2022-12-15 09:34:57 +01:00
parent 08dbff1008
commit de2ce7e9b0
4 changed files with 30 additions and 24 deletions

View File

@ -12,7 +12,7 @@ D 1
L 5 L 5
R 2"; R 2";
Answerable answerable = new Day09Part1(); Answerable answerable = new Day09Part2();
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile); byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
//dataArray = Encoding.UTF8.GetBytes(_demoData); //dataArray = Encoding.UTF8.GetBytes(_demoData);

View File

@ -14,7 +14,7 @@
string[] movements = GetAsStringArray(data); string[] movements = GetAsStringArray(data);
RopeWalker walker = new(); RopeWalker walker = new();
return walker.ProcessMovement(movements).ToString(); return walker.ProcessMovement(movements, 2).ToString();
} }
} }
} }

View File

@ -1,5 +1,6 @@
namespace AdventOfCodeLibrary._2022 namespace AdventOfCodeLibrary._2022
{ {
using AdventOfCodeLibrary._2022.Day_09;
using AdventOfCodeLibrary.Shared; using AdventOfCodeLibrary.Shared;
public class Day09Part2 : Answerable public class Day09Part2 : Answerable
@ -10,14 +11,10 @@
public override string GetAnswer(byte[] data) public override string GetAnswer(byte[] data)
{ {
int markerLength = 14; string[] movements = GetAsStringArray(data);
for (int skip = 0; skip < data.Length - 1 - markerLength; skip++) RopeWalker walker = new();
{
if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength)
return (skip + markerLength).ToString();
}
return "nothing found"; return walker.ProcessMovement(movements, 10).ToString();
} }
} }
} }

View File

@ -16,14 +16,18 @@
internal class RopeWalker internal class RopeWalker
{ {
private readonly VirtualPoint HeadLocation = new(); private readonly VirtualPoint HeadLocation = new();
private VirtualPoint LastHeadLocation = new();
private VirtualPoint TailLocation = new(); private VirtualPoint TailLocation = new();
private List<string> TailVistedLocations = new(); private List<string> TailVistedLocations = new();
internal int ProcessMovement(string[] movements) internal int ProcessMovement(string[] movements, int ropeLength)
{ {
VirtualPoint[] rope = new VirtualPoint[ropeLength];
for (int ropeIndex = 0; ropeIndex < rope.Length; ropeIndex++)
{
rope[ropeIndex] = new VirtualPoint();
}
// add the start position // add the start position
TailVistedLocations.Add($"0,0"); TailVistedLocations.Add($"0,0");
@ -34,27 +38,32 @@
for (int movement = 0; movement < amountToMove; movement++) for (int movement = 0; movement < amountToMove; movement++)
{ {
// set the last pos before moving
LastHeadLocation.X = HeadLocation.X;
LastHeadLocation.Y = HeadLocation.Y;
// do the actual move // do the actual move
switch (direction) switch (direction)
{ {
case 'U': HeadLocation.Translate(0, 1); break; case 'U': rope[0].Translate(0, 1); break;
case 'D': HeadLocation.Translate(0, -1); break; case 'D': rope[0].Translate(0, -1); break;
case 'R': HeadLocation.Translate(1, 0); break; case 'R': rope[0].Translate(1, 0); break;
case 'L': HeadLocation.Translate(-1, 0); break; case 'L': rope[0].Translate(-1, 0); break;
} }
// check if the tail is touching for (int ropeIndex = 1; ropeIndex < rope.Length; ropeIndex++)
if (!IsTailTouchingHead(HeadLocation, TailLocation))
{ {
TailLocation.Translate(Math.Sign(HeadLocation.X - TailLocation.X), Math.Sign(HeadLocation.Y - TailLocation.Y)); if (!IsTailTouchingHead(rope[ropeIndex - 1], rope[ropeIndex]))
{
rope[ropeIndex].Translate(Math.Sign(rope[ropeIndex - 1].X - rope[ropeIndex].X), Math.Sign(rope[ropeIndex - 1].Y - rope[ropeIndex].Y));
// add the new position to the list of visited positions // add the new position to the list of visited positions
TailVistedLocations.Add($"{TailLocation.X},{TailLocation.Y}");
continue;
} }
// if not touching nothing has changed so stop with checking
break;
}
TailVistedLocations.Add($"{rope[^1].X},{rope[^1].Y}");
} }
} }