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

Advent Of Code

996 readers
4 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] 2 points 1 month ago

C#

public class Day04 : Solver
{
  private int width, height;
  private char[,] data;

  public void Presolve(string input) {
    var lines = input.Trim().Split("\n").ToList();
    height = lines.Count;
    width = lines[0].Length;
    data = new char[height, width];
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        data[i, j] = lines[i][j];
      }
    }
  }

  private static readonly string word = "XMAS";

  public string SolveFirst()
  {
    int counter = 0;
    for (int start_i = 0; start_i < height; start_i++) {
      for (int start_j = 0; start_j < width; start_j++) {
        if (data[start_i, start_j] != word[0]) continue;
        for (int di = -1; di <= 1; di++) {
          for (int dj = -1; dj <= 1; dj++) {
            if (di == 0 && dj == 0) continue;
            int end_i = start_i + di * (word.Length - 1);
            int end_j = start_j + dj * (word.Length - 1);
            if (end_i < 0 || end_j < 0 || end_i >= height || end_j >= width) continue;
            for (int k = 1; k < word.Length; k++) {
              if (data[start_i + di * k, start_j + dj * k] != word[k]) break;
              if (k == word.Length - 1) counter++;
            }
          }
        }
      }
    }
    return counter.ToString();
  }

  public string SolveSecond()
  {
    int counter = 0;
    for (int start_i = 1; start_i < height - 1; start_i++) {
      for (int start_j = 1; start_j < width - 1; start_j++) {
        if (data[start_i, start_j] != 'A') continue;
        int even_mas_starts = 0;
        for (int di = -1; di <= 1; di++) {
          for (int dj = -1; dj <= 1; dj++) {
            if (di == 0 && dj == 0) continue;
            if ((di + dj) % 2 != 0) continue;
            if (data[start_i + di, start_j + dj] != 'M') continue;
            if (data[start_i - di, start_j - dj] != 'S') continue;
            even_mas_starts++;
          }
        }
        if (even_mas_starts == 2) counter++;
      }
    }
    return counter.ToString();
  }
}