From f18ebfcb217eda0910d53e1264ded66921e89302 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Wed, 4 Dec 2024 20:45:09 -0500 Subject: [PATCH] day 4 --- .../Problems/AOC2024/Day4/CeresSearch.cs | 130 ++++++++++++++++++ AdventOfCode/Program.cs | 1 + 2 files changed, 131 insertions(+) create mode 100644 AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs diff --git a/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs b/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs new file mode 100644 index 0000000..90d61be --- /dev/null +++ b/AdventOfCode/Problems/AOC2024/Day4/CeresSearch.cs @@ -0,0 +1,130 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Pos = (int x, int y); + +namespace AdventOfCode.Problems.AOC2024.Day4; +[ProblemInfo(2024, 4, "Ceres Search")] +internal class CeresSearch : Problem +{ + + private string[] _data = []; + private static Pos[] dirs = [ + (-1, 0), //Left + (-1, -1), //Top Left + (0, -1), //Top + (1, -1), //Top Right + (1, 0), //Right + (1, 1), //Bottom Right + (0, 1), //Bottom + (-1, 1), //Bottom Left + ]; + + private static Pos[] xdirs = [ + (-1, -1), //Top Left + (1, -1), //Top Right + (1, 1), //Bottom Right + (-1, 1), //Bottom Left + ]; + + public override void CalculatePart1() + { + Part1 = FindWord(_data, "XMAS"); + } + + public override void CalculatePart2() + { + Part2 = FindXWord(_data, "MAS"); + } + public override void LoadInput() + { + _data = ReadInputLines("input.txt"); + } + + private static int FindXWord(string[] data, string target) + { + var matches = 0; + var pivot = target.Length / 2; + var tgt = target[pivot]; + var branchA = string.Join("", target[..(pivot+1)].Reverse()); + var branchB = target[pivot..]; + for (int y = 0; y < data.Length; y++) + { + var row = data[y]; + for (int x = 0; x < row.Length; x++) + { + var c = row[x]; + if (c == tgt) + { + for (int i = 0; i < xdirs.Length; i++) + { + 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])) + { + 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])) + matches++; + } + } + } + } + } + return matches; + } + + private static int FindWord(string[] data, string target) + { + var matches = 0; + var tgt = target[0]; + for (int y = 0; y < data.Length; y++) + { + var row = data[y]; + for (int x = 0; x < row.Length; x++) + { + var c = row[x]; + if(c == tgt) + { + foreach (var dir in dirs) + { + if(CheckWord(data, (x, y), dir, target, 1, [c])) + matches++; + } + } + } + } + return matches; + } + + private static bool CheckWord(string[] data, Pos pos, Pos dir, string target, int targetPos, List? acc = null) + { + if (acc == null) + acc = []; + Pos curPos = (pos.x + dir.x, pos.y + dir.y); + + + if(curPos.y < 0 || curPos.y >= data.Length) + return false; + if (curPos.x < 0 || curPos.x >= data[0].Length) + return false; + + 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 false; + } + + + +} diff --git a/AdventOfCode/Program.cs b/AdventOfCode/Program.cs index 61d4f82..4786316 100644 --- a/AdventOfCode/Program.cs +++ b/AdventOfCode/Program.cs @@ -1,4 +1,5 @@ global using AdventOfCode.Runner; +global using AdventOfCode.Runner.Attributes; var runner = new AOCRunner(); runner.RenderInteractiveMenu();