this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

982 readers
59 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 3 weeks ago (1 children)

I tried to think of some clever LINQ to do this one, but was blanking entirely.

So naΓ―ve search it is.

C#

string wordsearch = "";
int width;
int height;

public void Input(IEnumerable<string> lines)
{
  wordsearch = string.Join("", lines);
  height = lines.Count();
  width = lines.First().Length;
}

public void Part1()
{
  int words = 0;
  for (int y = 0; y < height; y++)
    for (int x = 0; x < width; x++)
      words += SearchFrom(x, y);

  Console.WriteLine($"Words: {words}");
}
public void Part2()
{
  int words = 0;
  for (int y = 1; y < height - 1; y++)
    for (int x = 1; x < width - 1; x++)
      words += SearchCross(x, y);

  Console.WriteLine($"Crosses: {words}");
}

public int SearchFrom(int x, int y)
{
  char at = wordsearch[y * width + x];
  if (at != 'X')
    return 0;

  int words = 0;
  for (int ydir = -1; ydir <= 1; ++ydir)
    for (int xdir = -1; xdir <= 1; ++xdir)
    {
      if (xdir == 0 && ydir == 0)
        continue;

      if (SearchWord(x, y, xdir, ydir))
        words++;
    }

  return words;
}

private readonly string word = "XMAS";
public bool SearchWord(int x, int y, int xdir, int ydir)
{
  int wordit = 0;
  while (true)
  {
    char at = wordsearch[y * width + x];
    if (at != word[wordit])
      return false;

    if (wordit == word.Length - 1)
      return true;

    wordit++;

    x += xdir;
    y += ydir;

    if (x < 0 || y < 0 || x >= width || y >= height)
      return false;
  }
}

public int SearchCross(int x, int y)
{
  if (x == 0 || y == 0 || x == width - 1 || y == width - 1)
    return 0;

  char at = wordsearch[y * width + x];
  if (at != 'A')
    return 0;

  int found = 0;
  for (int ydir = -1; ydir <= 1; ++ydir)
    for (int xdir = -1; xdir <= 1; ++xdir)
    {
      if (xdir == 0 || ydir == 0)
        continue;

      if (wordsearch[(y + ydir) * width + (x + xdir)] != 'M')
        continue;
      if (wordsearch[(y - ydir) * width + (x - xdir)] != 'S')
        continue;

      found++;
    }

  if (found == 2)
    return 1;

  return 0;
}

[–] [email protected] 1 points 3 weeks ago

I haven't quite started yet, and this one does feel like a busy work kinda problem. I was wondering if I could write something to rotate the board and do the search, but I think that might be not worth the effort