From 9f7096e6bb8cca64eda8bda66f8ec72ff71621a3 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Wed, 4 Dec 2024 20:53:40 -0500 Subject: [PATCH] cleanup --- .../Problems/AOC2024/Day4/CeresSearch.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs b/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs index 90d61be..291f98c 100644 --- a/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs +++ b/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs @@ -13,7 +13,7 @@ internal class CeresSearch : Problem { private string[] _data = []; - private static Pos[] dirs = [ + private static readonly Pos[] dirs = [ (-1, 0), //Left (-1, -1), //Top Left (0, -1), //Top @@ -24,7 +24,7 @@ internal class CeresSearch : Problem (-1, 1), //Bottom Left ]; - private static Pos[] xdirs = [ + private static readonly Pos[] xdirs = [ (-1, -1), //Top Left (1, -1), //Top Right (1, 1), //Bottom Right @@ -52,10 +52,10 @@ internal class CeresSearch : Problem var tgt = target[pivot]; var branchA = string.Join("", target[..(pivot+1)].Reverse()); var branchB = target[pivot..]; - for (int y = 0; y < data.Length; y++) + for (int y = 1; y < data.Length-1; y++) { var row = data[y]; - for (int x = 0; x < row.Length; x++) + for (int x = 1; x < row.Length-1; x++) { var c = row[x]; if (c == tgt) @@ -65,11 +65,11 @@ internal class CeresSearch : Problem Pos dir = xdirs[i]; Pos opposingDir = xdirs[(i + 2) % xdirs.Length]; - if (CheckWord(data, (x, y), dir, branchA, 1, [c]) && CheckWord(data, (x,y), opposingDir, branchB, 1, [c])) + if (CheckWord(data, (x, y), dir, branchA, 1) && CheckWord(data, (x,y), opposingDir, branchB, 1)) { Pos dir2 = xdirs[(i+1) % xdirs.Length]; Pos opposingDir2 = xdirs[(i + 3) % xdirs.Length]; - if (CheckWord(data, (x, y), dir2, branchA, 1, [c]) && CheckWord(data, (x, y), opposingDir2, branchB, 1, [c])) + if (CheckWord(data, (x, y), dir2, branchA, 1) && CheckWord(data, (x, y), opposingDir2, branchB, 1)) matches++; } } @@ -93,7 +93,7 @@ internal class CeresSearch : Problem { foreach (var dir in dirs) { - if(CheckWord(data, (x, y), dir, target, 1, [c])) + if(CheckWord(data, (x, y), dir, target, 1)) matches++; } } @@ -102,10 +102,8 @@ internal class CeresSearch : Problem return matches; } - private static bool CheckWord(string[] data, Pos pos, Pos dir, string target, int targetPos, List? acc = null) + private static bool CheckWord(string[] data, Pos pos, Pos dir, string target, int targetPos) { - if (acc == null) - acc = []; Pos curPos = (pos.x + dir.x, pos.y + dir.y); @@ -117,10 +115,9 @@ internal class CeresSearch : Problem var c = data[curPos.y][curPos.x]; if(c == target[targetPos]) { - acc.Add(c); if (targetPos == target.Length - 1) return true; - return CheckWord(data, curPos, dir, target, targetPos + 1, acc); + return CheckWord(data, curPos, dir, target, targetPos + 1); } return false; }