top of page
Search

Advent of Code 2024 (Go) - Day 4: Read your prompts

  • Writer: John Montroy
    John Montroy
  • Dec 9, 2024
  • 2 min read
I thought this was cute.
I thought this was cute.

I wrote a working algorithm to recursively find every version of XMAS IF you were allowed to change directions each time. So I misread the prompt. I thought this was valid:

X - - -
- M - -
- - A -
- S - -

This meant checking every direction for a valid next letter, every single time. Which (a) was a natural fit for recursion, and (b) generated way too many matches, and (c) literally isn't the problem, lol.


Takeaways today:


  • Read your problem thoroughly. Read examples.

  • Data structures can help simplify your search logic, at the expense of space. Plus, the verbosity lets users see exactly where and what you're searching for. I'm trying to reach for intermediate data structures more and more - they help debug via intermediate state, and can often really simplify the logic (because instead you just store everything you need on an object and do all your operations at once).

    • It probably would've been even easier to just make a map of the X around each 'A', including position and rune per corner, and then just compared that to the valid configurations. A little silly-feeling, but very clear.

  • If you're ever nesting loops very deeply, or reaching for loop labels, make a function instead.

  • It's okay to be a bit redundant for the sake of simplicity. I over-check both part 1 and part 2, but being more efficient (fewer loops) would require managing more state for part 2. For part 1, it'd just be an early-exit, which isn't at all a big deal and is totally a low-hanging fruit win.


Not too bad here! Once I read the prompt right, it didn't take long.

Comments


bottom of page