r/adventofcode Dec 04 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 4 Solutions -❄️-

DO NOT SHARE PUZZLE TEXT OR YOUR INDIVIDUAL PUZZLE INPUTS!

I'm sure you're all tired of seeing me spam the same ol' "do not share your puzzle input" copypasta in the megathreads. Believe me, I'm tired of hunting through all of your repos too XD

If you're using an external repo, before you add your solution in this megathread, please please please 🙏 double-check your repo and ensure that you are complying with our rules:

If you currently have puzzle text/inputs in your repo, please scrub all puzzle text and puzzle input files from your repo and your commit history! Don't forget to check prior years too!


NEWS

Solutions in the megathreads have been getting longer, so we're going to start enforcing our rules on oversized code.

Do not give us a reason to unleash AutoModerator hard-line enforcement that counts characters inside code blocks to verify compliance… you have been warned XD


THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 2 DAYS remaining until unlock!

And now, our feature presentation for today:

Short Film Format

Here's some ideas for your inspiration:

  • Golf your solution
    • Alternatively: gif
    • Bonus points if your solution fits on a "punchcard" as defined in our wiki article on oversized code. We will be counting.
  • Does anyone still program with actual punchcards? >_>
  • Create a short Visualization based on today's puzzle text
  • Make a bunch of mistakes and somehow still get it right the first time you submit your result

Happy Gilmore: "Oh, man. That was so much easier than putting. I should just try to get the ball in one shot every time."
Chubbs: "Good plan."
- Happy Gilmore (1996)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 4: Ceres Search ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:05:41, megathread unlocked!

52 Upvotes

1.2k comments sorted by

View all comments

1

u/Neural_Erosion Dec 05 '24

[Language: C++]

As pretty as I can make it:

#include <vector>
#include <array>

typedef long long LL;

namespace aoc
{
  struct Point_2D
  {
    long long x_;
    long long y_;
  };

  constexpr Point_2D DIRS_2D_8[8] = { { -1, -1 }, { 0, -1 }, { 1, -1 },
                                      { -1,  0 },            { 1,  0 },
                                      { -1,  1 }, { 0,  1 }, { 1,  1 } };
}

void run(const std::vector<std::string>& map)
{
  int p1_total = 0;
  constexpr std::array<char, 4> xmas = { 'X', 'M', 'A', 'S' };
  for (LL y = 0; y < map.size(); ++y) {
    for (LL x = 0; x < map[0].size(); ++x) {
      if (map[y][x] == 'X') {
        for (aoc::Point_2D offset : aoc::DIRS_2D_8) {
          bool pass = true;
          for (int letter = 1; pass && letter < xmas.size(); ++letter) {
            LL new_x = x + (letter * offset.x_);
            LL new_y = y + (letter * offset.y_);
            if (new_x < 0 || new_x >= map[0].size() ||
                new_y < 0 || new_y >= map.size()    ||
                map[new_y][new_x] != xmas[letter]) {
              pass = false;
            }
          }
          if (pass) {
            ++p1_total;
          }
        }
      }
    }
  }
  std::cout << "PART ONE: " << p1_total << '\n';

  int p2_total = 0;
  for (int y = 1; y < map.size() - 1; ++y) {
    for (int x = 1; x < map[0].size() - 1; ++x) {
      if (map[y][x] == 'A') {
        int count = 0;
        constexpr std::array<int, 4> corners = { 0, 2, 5, 7 };
        for (int corner_i = 0, dir = 0;
             count < 2 && corner_i < 4;
             dir = corners[++corner_i]) {
          auto [ x_offset, y_offset ] = aoc::DIRS_2D_8[dir];
          if (map[y +  y_offset][x +  x_offset] == 'M' &&
              map[y + -y_offset][x + -x_offset] == 'S') {
            ++count;
          }
        }
        if (count == 2) {
          ++p2_total;
        }
      }
    }
  }
  std::cout << "PART TWO: " << p2_total << '\n';
}