Completed Day 9 p 2
This commit is contained in:
parent
08dbff1008
commit
de2ce7e9b0
@ -12,7 +12,7 @@ D 1
|
||||
L 5
|
||||
R 2";
|
||||
|
||||
Answerable answerable = new Day09Part1();
|
||||
Answerable answerable = new Day09Part2();
|
||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
string[] movements = GetAsStringArray(data);
|
||||
RopeWalker walker = new();
|
||||
|
||||
return walker.ProcessMovement(movements).ToString();
|
||||
return walker.ProcessMovement(movements, 2).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
namespace AdventOfCodeLibrary._2022
|
||||
{
|
||||
using AdventOfCodeLibrary._2022.Day_09;
|
||||
using AdventOfCodeLibrary.Shared;
|
||||
|
||||
public class Day09Part2 : Answerable
|
||||
@ -10,14 +11,10 @@
|
||||
|
||||
public override string GetAnswer(byte[] data)
|
||||
{
|
||||
int markerLength = 14;
|
||||
for (int skip = 0; skip < data.Length - 1 - markerLength; skip++)
|
||||
{
|
||||
if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength)
|
||||
return (skip + markerLength).ToString();
|
||||
}
|
||||
string[] movements = GetAsStringArray(data);
|
||||
RopeWalker walker = new();
|
||||
|
||||
return "nothing found";
|
||||
return walker.ProcessMovement(movements, 10).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,14 +16,18 @@
|
||||
internal class RopeWalker
|
||||
{
|
||||
private readonly VirtualPoint HeadLocation = new();
|
||||
private VirtualPoint LastHeadLocation = new();
|
||||
|
||||
private VirtualPoint TailLocation = 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
|
||||
TailVistedLocations.Add($"0,0");
|
||||
|
||||
@ -34,27 +38,32 @@
|
||||
|
||||
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
|
||||
switch (direction)
|
||||
{
|
||||
case 'U': HeadLocation.Translate(0, 1); break;
|
||||
case 'D': HeadLocation.Translate(0, -1); break;
|
||||
case 'R': HeadLocation.Translate(1, 0); break;
|
||||
case 'L': HeadLocation.Translate(-1, 0); break;
|
||||
case 'U': rope[0].Translate(0, 1); break;
|
||||
case 'D': rope[0].Translate(0, -1); break;
|
||||
case 'R': rope[0].Translate(1, 0); break;
|
||||
case 'L': rope[0].Translate(-1, 0); break;
|
||||
}
|
||||
|
||||
// check if the tail is touching
|
||||
if (!IsTailTouchingHead(HeadLocation, TailLocation))
|
||||
for (int ropeIndex = 1; ropeIndex < rope.Length; ropeIndex++)
|
||||
{
|
||||
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
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user