From 7f4f514b8c0676a294303d71eb3d2db377c961d2 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sat, 7 Dec 2024 13:11:03 -0600 Subject: [PATCH] fix: improve day 6 part 2 solution...moved from 40s to 15s by only looking at places on guard's existing path. --- 06/GuardGallivant/Map.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/06/GuardGallivant/Map.cs b/06/GuardGallivant/Map.cs index c7a0d8e..2529d0e 100644 --- a/06/GuardGallivant/Map.cs +++ b/06/GuardGallivant/Map.cs @@ -28,14 +28,16 @@ class Map public int IdentifyNumberOfPlacementsForNewObstruction() { var count = 0; - - WalkMap((rowIndex, columnIndex) => + + var guardPathPositions = GetDistinctGuardPositions(); + + foreach (var (columnIndex, rowIndex) in guardPathPositions) { var currentCharacter = _input[rowIndex][columnIndex]; if (IsGuard(currentCharacter) || IsBlocked(currentCharacter)) { - return; + continue; } var mapWithNewObstruction = _input.ToArray(); @@ -49,7 +51,7 @@ class Map { count++; } - }); + } return count; } @@ -95,9 +97,12 @@ class Map } } - public int PredictDistinctPositionsCount() + public int PredictDistinctPositionsCount() => GetDistinctGuardPositions().Count; + + private HashSet<(int x, int y)> GetDistinctGuardPositions() { var currentGuardPosition = GetGuardPosition(); + var uniquePositions = new HashSet<(int x, int y)>() { (currentGuardPosition.ColumnIndex, currentGuardPosition.RowIndex), @@ -129,7 +134,7 @@ class Map uniquePositions.Add((currentGuardPosition.ColumnIndex, currentGuardPosition.RowIndex)); } - return uniquePositions.Count; + return uniquePositions; } private GuardPosition GetGuardPosition()