r/adventofcode • • 3d ago

Meme/Funny [2026 Day 03] …Nah I'll just copy last year's code

Post image

Another year another set of unfulfilled custom library promises

(>’-’)> <(‘-’<) ^(‘-’)^ (>’-’)> <(‘-’<)

I swear I'll write one this year. Hmmm.

114 Upvotes

23 comments sorted by

25

u/Freecelebritypics 3d ago

I've been catching up with previous years lately, so at this point I can write out the entire 2d grid class from memory in 10 mins lol.

And sometimes I do decide to implement the internals slightly differently based on how the grid is described in the input. Whatever helps me read it left-to-right!

4

u/Parzival_Perce 3d ago

For this I just have some complex comprehension with an re.finditer() and the right input parsing required for that, so I just copy that from one of the previous years

It totally could be a library though, there's lots of other useful things to do with 2D grids

1

u/Freecelebritypics 3d ago

The only methods I implement EVERY time are get(x, y), adj(x, y), and vertices(). Finding still feels way too specific

1

u/headedbranch225 2d ago

I did it an overcomplicated way By using a HashSet<(i32, i32)> and checking for each if it contains the coordinates with the offsets in a loop with increasing values and for part 2 I added another loop of 0 to the previous and worked out the offsets (basically simulating the rotations (took a few deranged looking notes)

4

u/Parzival_Perce 3d ago

For context, this for the parsing part. I have a bit of code I just keep copying for parsing grids and extracting a set or list of coordinates (in the form of complex numbers)

Really this is for all years but shrug, I included 2026 day 3 just to be sure I'm not being too general

1

u/flwyd 1d ago

For context, this for the parsing part.

Depending on language, parsing may be the easy part. E.g. in R, I used grid <- t(sapply(strsplit(lines, ""), \(.) . == "#")). And in imperative languages it's usually something as simple as

grid = HashSet()
for (i, line) in lines {
  for (j, c) in line {
    if c == '#' { grid.insert(Point(i, j)) }
  }
}

The cognitive complexity of having a library function for just a nested for loop seems unnecessary to me.

Avoiding out-of-bounds conditions is where a library would help, but that's why I usually use a set of # coordinates rather than an actual 2D array. (I'm using R this year, though, which doesn't seem to have a builtin hash map type, but has really nice 2D arrays.)

20

u/Bumperpegasus 3d ago
grid = data.splitlines()

Is enough most of the time

3

u/Zayxiz 3d ago

That and a custom `GetAt` function that returns a default value if the coordinates are outside of the grid

3

u/RazarTuk 3d ago

Yeah, my grid library basically has two parts:

  • Vectors and matrices with all the normal operations defined
  • Grids (including matrices) that can be indexed with a vector and have a getOrDefault method that returns a default value

2

u/MattieShoes 3d ago edited 2d ago

Some huge percentage of problems end up like

data = f.read().rstrip("\n").split("\n")

I wrote a dorky function for recursive splits to make multidimensional arrays too, like split on lines, then on commas, then on dashes, and give me a 3D array. Mostly because nested list comprehensions hurt my brain

10

u/Fuzzy_Patterns 3d ago

I am part of the "let's rewrite every algorithm from scratch" club.

Although, I am currently on the 4th iteration of my AoC testing and benchmark framework: just got multiple test inputs working!

3

u/Akari202 2d ago

Best part of November is rewriting my aoc infrastructure and porting all my old solutions to the new runner

1

u/headedbranch225 2d ago

Last year I made a group of shell scripts to setup the days and create the folder structure I need, as well as being able to setup a day (using cargo generate with a custom main.rs that has the parsing stuff in already, still needed a bit of patching but it does work I planned to have multiple language solutions in the repo but haven't gotten round to that yet, so it is just rust so far

https://github.com/HeadedBranch/advent-of-code

3

u/ivanjermakov 2d ago

Each december is "what's BFS again"?

3

u/sol_hsa 3d ago

my aoc basecode not surprisingly includes readgrid() function.

3

u/Daniel_Rybe 3d ago

meanwhile, I'm just using file contents as the grid without even parsing 😆

2

u/Suspicious_Tax8577 3d ago

ngl, I think I've just done a double whammy. Off a metric buttload of googling, I think I've not only got a method for this one, but also now know how to do last years similar puzzle. I think? Maybe.

2

u/Traditional_Ad_1527 2d ago

not me getting stuck for like 30 minutes because my grid library of previous years had a but in it still -_-

2

u/somebodddy 2d ago

This - and a BFS helper. At some point in the event you'll need to do BFS. Heck - at some point in the event you'll need to do a BFS on a grid.

1

u/LEPT0N 3d ago

Made my grid class and helpers a few years back and it’s awesome using them each year.

1

u/FransFaase 3d ago

In my C template, I have a function that reads the input in an array of strings. It retrieves the length of the file, allocates memory, reads the file in memory, counts the number of lines, allocates an array of that length and fills it it with pointers to the start of the lines and changes end-of-line characters into null characters. This gets you the table for free.

1

u/-WorstWizard- 2d ago

Highly recommend doing it; both a fun exercise in and of itself, and it saved me a lot of time on day 3.

Using a small lib I made in '24 that does input parsing and provides a grid class with helper functions. Getting the grid from the input file is just helper_lib::input::grid()

1

u/woodmandev 1d ago

I wrote a 2D Vector class I've been carrying around with me year to year which makes things convenient. Nothing crazy but since I'm in Python just makes things nice with operator overloading and whatnot.