Skip to content

Commit

Permalink
Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadej Koderman committed Dec 5, 2024
1 parent c001785 commit 74283d4
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 5 deletions.
8 changes: 7 additions & 1 deletion AoCCore/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Expressions;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace AoCCore;
Expand Down Expand Up @@ -51,4 +52,9 @@ public static void P<T>(this IEnumerable<T> values, string message)
public static long Long(this string input) => input.As<long>();

public static string Join(this IEnumerable<string> input, string separator = ",") => string.Join(separator, input);

public static MatchCollection Matches(this string input, [StringSyntax("Regex")] string pattern)
{
return new Regex(pattern).Matches(input);
}
}
122 changes: 118 additions & 4 deletions AoCCore/Y2024.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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
Expand All @@ -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()
{
}
Expand Down

0 comments on commit 74283d4

Please sign in to comment.