Skip to content

Commit

Permalink
2024/04
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 4, 2024
1 parent 4c568ae commit b316bdc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 21 deletions.
8 changes: 8 additions & 0 deletions 2024/Day04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## --- Day 4: Ceres Search ---
"Looks like the Chief's not here. Next!" One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the [Ceres monitoring station](/2019/day/10)!

As the search for the Chief continues, a small Elf who lives on the station tugs on your shirt; she'd like to know if you could help her with her <em>word search</em> (your puzzle input). She only has to find one word: <code>XMAS</code>.

This word search allows words to be horizontal, vertical, diagonal, written backwards, or even overlapping other words. It's a little unusual, though, as you don't merely need to find one instance of <code>XMAS</code> - you need to find <em>all of them</em>. Here are a few ways <code>XMAS</code> might appear, where irrelevant characters have been replaced with <code>.</code>:

Read the [full puzzle](https://adventofcode.com/2024/day/4).
61 changes: 61 additions & 0 deletions 2024/Day04/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace AdventOfCode.Y2024.Day04;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Numerics;
using System.Linq;

using Map = System.Collections.Immutable.ImmutableDictionary<System.Numerics.Complex, char>;

[ProblemName("Ceres Search")]
class Solution : Solver {

Complex Up = -Complex.ImaginaryOne;
Complex Down = Complex.ImaginaryOne;
Complex Left = -1;
Complex Right = 1;

public object PartOne(string input) {
var mat = GetMap(input);
return (
from pt in mat.Keys
from dir in new[] { Right, Right + Down, Down + Left, Down}
where Matches(mat, pt, dir, "XMAS")
select 1
).Count();
}

public object PartTwo(string input) {
var mat = GetMap(input);
return (
from pt in mat.Keys
where
Matches(mat, pt + Up + Left, Down + Right, "MAS") &&
Matches(mat, pt + Down + Left, Up + Right, "MAS")
select 1
).Count();
}

// check if the pattern (or its reverse) can be read in the given direction
// starting from pt
bool Matches(Map map, Complex pt, Complex dir, string pattern) {
var chars = Enumerable.Range(0, pattern.Length)
.Select(i => map.GetValueOrDefault(pt + i * dir))
.ToArray();
return
Enumerable.SequenceEqual(chars, pattern) ||
Enumerable.SequenceEqual(chars, pattern.Reverse());
}

// store the points in a dictionary so that we can iterate over them and
// to easily deal with points outside the area using GetValueOrDefault
Map GetMap(string input) {
var map = input.Split("\n");
return (
from y in Enumerable.Range(0, map.Length)
from x in Enumerable.Range(0, map[0].Length)
select new KeyValuePair<Complex, char>(Complex.ImaginaryOne * y + x, map[y][x])
).ToImmutableDictionary();
}
}
Binary file added 2024/Day04/input.in
Binary file not shown.
2 changes: 2 additions & 0 deletions 2024/Day04/input.refout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2414
1871
53 changes: 36 additions & 17 deletions 2024/SplashScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public void Show() {

var color = Console.ForegroundColor;
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ {'year': 2024}\n ");
Write(0xcc00, false, " \n ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ $year = 2024\n ");
Write(0xcc00, false, "\n ");
Write(0xcccccc, false, ".--'");
Write(0xe3b585, false, "~ ~ ~");
Write(0xcccccc, false, "| .-' ");
Expand Down Expand Up @@ -47,21 +47,40 @@ public void Show() {
Write(0x886655, false, "__------");
Write(0xcccccc, false, "| 3 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, "| | | | ");
Write(0x666666, false, " 4\n 5\n ");
Write(0x666666, false, " 6\n 7\n ");
Write(0x666666, false, " 8\n ");
Write(0x666666, false, " 9\n 10\n ");
Write(0x666666, false, " 11\n ");
Write(0x666666, false, " 12\n 13\n ");
Write(0x666666, false, " 14\n ");
Write(0x666666, false, " 15\n 16\n ");
Write(0x666666, false, " 17\n ");
Write(0x666666, false, " 18\n 19\n ");
Write(0x666666, false, " 20\n ");
Write(0x666666, false, " 21\n 22\n ");
Write(0x666666, false, " 23\n ");
Write(0x666666, false, "24\n 25\n \n");
Write(0xcccccc, false, "|");
Write(0x1461f, false, "@");
Write(0x5eabb4, false, "..");
Write(0x7fbd39, false, "@");
Write(0xe3b585, false, "'. ~ ");
Write(0xcc00, false, "\" ' ");
Write(0xe3b585, false, "~ ");
Write(0xcccccc, false, "| |");
Write(0x9900, false, ">");
Write(0x66ff, true, "O");
Write(0x9900, false, ">");
Write(0xff9900, true, "o");
Write(0x9900, false, "<");
Write(0xff0000, true, "@");
Write(0x9900, false, "< ");
Write(0x886655, false, "\\____ ");
Write(0xcc00, false, ".'");
Write(0xcccccc, false, "| 4 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, "| | | .. | ");
Write(0x666666, false, " 5\n 6\n ");
Write(0x666666, false, " 7\n 8\n ");
Write(0x666666, false, " 9\n ");
Write(0x666666, false, " 10\n 11\n ");
Write(0x666666, false, " 12\n ");
Write(0x666666, false, " 13\n 14\n ");
Write(0x666666, false, " 15\n ");
Write(0x666666, false, " 16\n 17\n ");
Write(0x666666, false, " 18\n ");
Write(0x666666, false, " 19\n 20\n ");
Write(0x666666, false, " 21\n ");
Write(0x666666, false, " 22\n 23\n ");
Write(0x666666, false, " 24\n ");
Write(0x666666, false, "25\n \n");

Console.ForegroundColor = color;
Console.WriteLine();
Expand Down
8 changes: 4 additions & 4 deletions 2024/calendar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b316bdc

Please sign in to comment.