You can find the puzzle inputs here: https://adventofcode.com/2024
- A good reminder that
.
by default matches any character except a new line. So, I learned about DOTALL flag. - I came across this website: https://regexcrossword.com/
- The better solution is to use sorting with a custom comparator function →
- Used
time python Day_06/task_6b.py
to measure the execution time of the script
- The go-to solution:
itertools.product
-
Using the next iterator from
itertools
→combinations
-
A cleaner way to handle this kind of logic. Instead of writing:
if cell not in antenna_positions:
antenna_positions[cell] = [(row_ind, col_ind)]
else:
antenna_positions[cell].append((row_ind, col_ind))
We can refactor it to be more concise:
antenna_positions.setdefault(cell, []).append((row_ind, col_ind))
Alternatively, we can use defaultdict
from collections
, which automatically provides a default value for the key that doesn't exist.
In part two, I got rid of OrderedDict
since I hadn't actually used it in part one anyway.
To improve performance, I changed the structure from index: file_id
to file_id: [list of indices]
, as searching through the old dictionary was painfully slow.
I also replaced the while loop with a for loop (always a pleasure) 😇
Been there, done that 😅
The choice of a dynamic list as a data container was not the best. It turns out that order was not important at all, and it would have been much better to use a hash map where the key would be the engraved number and the value the number of occurrences.
I also read that returning from a with
block (e.g., when opening a file) works fine, and the file is automatically closed. The with
statement ensures cleanup happens, whether the block exits normally or via a return. 🔗
I probably would have done better if my puzzle_input
had been correct instead of last year's 😅
But this was one of those tasks I really liked (aside from the wrong input), where I had to grab a sheet of paper and a pen to plan it out.
What I’m proud of is that I came up with the correct perimeter formula right away. Even though it’s a bit basic, it works!
The match statement is a more elegant alternative to if...elif...else
✨
Brute force was good enough, so I stuck with it. Apparently, you can practically reuse part 2 from day 16 (you just need to remove the condition about 1000 points → 🔗), but I don’t have that part ready yet, so we’ll see in the future 😅
Part 1: I forgot that two picoseconds are needed for the cheating, so the distance isn’t simply the difference between the end position index and the start position index—we need to reduce it by 2.