From 74283d420c4448218e744dffe0f92e86476454f5 Mon Sep 17 00:00:00 2001 From: Tadej Koderman Date: Wed, 4 Dec 2024 06:43:16 +0100 Subject: [PATCH] Day 4 --- AoCCore/Extensions.cs | 8 ++- AoCCore/Y2024.cs | 122 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 5 deletions(-) diff --git a/AoCCore/Extensions.cs b/AoCCore/Extensions.cs index 1b7ae32..6eb3b8f 100644 --- a/AoCCore/Extensions.cs +++ b/AoCCore/Extensions.cs @@ -1,4 +1,5 @@ -using System.Linq.Expressions; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace AoCCore; @@ -51,4 +52,9 @@ public static void P(this IEnumerable values, string message) public static long Long(this string input) => input.As(); public static string Join(this IEnumerable input, string separator = ",") => string.Join(separator, input); + + public static MatchCollection Matches(this string input, [StringSyntax("Regex")] string pattern) + { + return new Regex(pattern).Matches(input); + } } \ No newline at end of file diff --git a/AoCCore/Y2024.cs b/AoCCore/Y2024.cs index be76b46..256181f 100644 --- a/AoCCore/Y2024.cs +++ b/AoCCore/Y2024.cs @@ -129,8 +129,9 @@ static bool IsSafe(int[] report) */ public static void Day03A() { - new Regex(@"mul\((\d+),(\d+)\)") - .Matches(Read.StringBatch().Join()) + Read.StringBatch() + .Join() + .Matches(@"mul\((\d+),(\d+)\)") .Sum(m => m.Groups[1].Value.Long() * m.Groups[2].Value.Int()) .P("Sum"); @@ -143,8 +144,9 @@ public static void Day03A() */ public static void Day03B() { - new Regex(@"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))") - .Matches(string.Join(",", Read.StringBatch())) + Read.StringBatch() + .Join() + .Matches(@"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))") .Aggregate( (Sum: 0L, Enabled: true), (a, m) => m.Groups[3].Success @@ -160,6 +162,118 @@ public static void Day03B() // 80570939 } + /* +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX + + */ + public static void Day04A() + { + var input = Read.StringBatch().ToArray(); + + var h = input.Length; if (h == 0) return; + var w = input[0].Length; + + var count = 0; + + for (int i = 0; i < h; i++) + { + for (int j = 0; j < w; j++) + { + if (input[i][j] != 'X') + continue; + + if (All(1, 0)) count++; // D + if (All(1, 1)) count++; // DR + if (All(0, 1)) count++; // R + if (All(-1, 1)) count++; // UR + if (All(-1, 0)) count++; // U + if (All(-1, -1)) count++; // UL + if (All(0, -1)) count++; // L + if (All(1, -1)) count++; // DL + + bool All(int y, int x) + { + return Check(1 * y, 1 * x, 'M') + && Check(2 * y, 2 * x, 'A') + && Check(3 * y, 3 * x, 'S'); + } + + bool Check(int y, int x, char c) + { + return i + y >= 0 + && i + y < h + && j + x >= 0 + && j + x < w + && input[i + y][j + x] == c; + } + } + } + + count.P(); + } + + public static void Day04B() + { + var input = Read.StringBatch().ToArray(); + + var h = input.Length; if (h == 0) return; + var w = input[0].Length; + + var count = 0; + + for (int i = 0; i < h; i++) + { + for (int j = 0; j < w; j++) + { + if (input[i][j] != 'M') + continue; + + // if (All(1, 0)) count++; // D + if (All(1, 1)) count++; // DR + // if (All(0, 1)) count++; // R + if (All(-1, 1)) count++; // UR + // if (All(-1, 0)) count++; // U + if (All(-1, -1)) count++; // UL + // if (All(0, -1)) count++; // L + if (All(1, -1)) count++; // DL + + bool All(int y, int x) + { + return Check(1 * y, 1 * x, 'A') + && Check(2 * y, 2 * x, 'S') + && + ( + Check(2 * y, 0 * x, 'M') + && Check(0 * y, 2 * x, 'S') + || + Check(2 * y, 0 * x, 'S') + && Check(0 * y, 2 * x, 'M') + ); + } + + bool Check(int y, int x, char c) + { + return i + y >= 0 + && i + y < h + && j + x >= 0 + && j + x < w + && input[i + y][j + x] == c; + } + } + } + + (count / 2).P(); + } + public static void Current() // Day05A() { }