Advent of Code - Day 5 - Printer Updates (Bash)

(task | solution)

It’s the fifth day of our outrageous adventure, and we are dealing with printer updates. To solve this riddle, we are getting back to the roots and our good ol’ friend Bash!

The good news here is that I don’t need to set up any particular environment because, thankfully, Bash is available on most systems out of the box.

Solution to the First Part

The solution is quite straightforward: We need to read the rules and the updates into an array, loop through all updates, and check if they somehow break the rules. That’s nothing too complicated. The only thing that I am probably doing unconventionally is how I check.

I am using a regular expression that is the opposite of the rule. If it matches, I know the update is invalid:

61.*?(,29)(,|$)

In this example, 29 is the actual rule name, and 61 should come after 29. With .*?, I am ungreedy checking for any characters in between, and the closing group just makes sure that after 29, there’s either another number or the end of the update.

Simple thing.

Solution to the Second Part

The second part looks way easier. They just want us to switch the position of the pages that we identified as erroneous.

Basically, it’s just something like this:

updates[page_index]=$subsequent_page
updates[subsequent_page_index]=$page

But… yeah, that’s the first bump in my journey. My code isn’t resulting in the correct answer, and it seems like I am missing a detail. To avoid wasting any more time, I’ll proceed to the next day… sorry folks :(

Whats up, Bash?

Rating: 8/12 – weird operators and syntax

The boss of all scripting languages: Bash. I still don’t understand most of the syntax rules, but thanks to AI, it’s not too hard to get things done in Bash. When working as a sysadmin, I loved automating processes and even building small user interfaces to make life on the command line easier. However, I guess there are better ways for that.

See you next day!