From de2ce7e9b02d7d714fccd7980e9e2652c0136e5b Mon Sep 17 00:00:00 2001 From: Rob Stoffelen Date: Thu, 15 Dec 2022 09:34:57 +0100 Subject: [PATCH] Completed Day 9 p 2 --- Advend Of Code Runner/Program.cs | 2 +- .../2022/Day 09/Day09Part1.cs | 2 +- .../2022/Day 09/Day09Part2.cs | 11 ++---- .../2022/Day 09/RopeWalker.cs | 39 ++++++++++++------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Advend Of Code Runner/Program.cs b/Advend Of Code Runner/Program.cs index 1fae6ed..d6738d9 100644 --- a/Advend Of Code Runner/Program.cs +++ b/Advend Of Code Runner/Program.cs @@ -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); diff --git a/Advent Of Code Library/2022/Day 09/Day09Part1.cs b/Advent Of Code Library/2022/Day 09/Day09Part1.cs index eae0de9..25c9465 100644 --- a/Advent Of Code Library/2022/Day 09/Day09Part1.cs +++ b/Advent Of Code Library/2022/Day 09/Day09Part1.cs @@ -14,7 +14,7 @@ string[] movements = GetAsStringArray(data); RopeWalker walker = new(); - return walker.ProcessMovement(movements).ToString(); + return walker.ProcessMovement(movements, 2).ToString(); } } } diff --git a/Advent Of Code Library/2022/Day 09/Day09Part2.cs b/Advent Of Code Library/2022/Day 09/Day09Part2.cs index f3f07db..d538483 100644 --- a/Advent Of Code Library/2022/Day 09/Day09Part2.cs +++ b/Advent Of Code Library/2022/Day 09/Day09Part2.cs @@ -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(); } } } diff --git a/Advent Of Code Library/2022/Day 09/RopeWalker.cs b/Advent Of Code Library/2022/Day 09/RopeWalker.cs index 0e65621..98f2c13 100644 --- a/Advent Of Code Library/2022/Day 09/RopeWalker.cs +++ b/Advent Of Code Library/2022/Day 09/RopeWalker.cs @@ -16,14 +16,18 @@ internal class RopeWalker { private readonly VirtualPoint HeadLocation = new(); - private VirtualPoint LastHeadLocation = new(); - private VirtualPoint TailLocation = new(); private List 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}"); + // add the new position to the list of visited positions + + continue; + } + + // if not touching nothing has changed so stop with checking + break; } + + TailVistedLocations.Add($"{rope[^1].X},{rope[^1].Y}"); } }