Skip to content

The Advent of Code 2021 Day 5 log, pointless points

Posted in Advent of Code

Hello everyone, it’s time for your Advent of Code 2021 Day 5 log. Never have I played myself more than in this season. Today the problem was fairly simple, figure whether or not a dot is on a line. Nothing more, nothing less. Actually a bit more, we had to count intersections and return the number of times more than two lines intersected. But still, you can boil down the tricky part to figuring whether a point is on a line and count that. Maybe the simplicity bothered me? Either way, let’s jump into it.

The problem was pretty simple, we had to parse some input and turn it into a collection of lines. Here, we define lines in the geometric sense, with a start and an end. I wrote that shit in seconds, there’s a reason my peers nicknamed me “Parserman” back at school. If not obvious, it was due to my ability to understand inputs and turn them into data structures. Felt super easy, barely an inconvenience at the time even though the Advent of Code makes me doubt myself sometimes.

Then when it came to determining that a certain point was on a given line I tried to go hard and write a version supporting diagonals. The code would make sure that the start would always be preceding the end. It worked as long as I ignored diagonals but I knew these would come in later. Then I realised that for the purpose of the first part, I didn’t need to consider diagonals. The problem highlighted “ignore diagonals” or something. At this point, I commented out that the diagonals handling code and the test passed. The ninth star of this season felt ripe for the taking and running against the input yielded zero.

Back to checking the input, which I didn’t check beforehand. The mistake was obvious, I assumed that the plane would be a 10×10 plane like in the sample data. I played myself once more. But this time the fix is easy. All I need to do is replace my grid side constant to use a variable when creating the grid. I can find this variable by selecting the maximum value between the biggest x and the biggest y from the lines. I ran the unit test just to double-check, it still passed. At this point I run the input, the answer looks legit and boom! Nine stars! Now I can breathe, I got a star, wrapping this up will be easy.

And there it is, the diagonal check. I figured I could just use Line2D to figure this one out but that would feel like cheating. I tried a few different things but eventually, the slope implementation was the one. Basically, you would check whether the line between the start and your point is contained by the line between the start and the end.

I ran my part one test which failed successfully. As in it returned the result for part two. I ran the code against the input and it worked! Ten stars. Since I had some extra time compared to what I feared, I cleaned up the code so that the tests for both parts can be run concurrently. I only had to pass a predicate to my grid generation method. One for part one and one for part two. Worked like a charm. And now you can enjoy that piece of throwaway code on my repository.

Thank you for reading my Advent of Code 2021 Day 5 log, I will see you tomorrow. As usual, I will push the code to my repo for this year on Github. Feel free to check out my Day 4 log right here.

Photo by Nils from Pexels

Be First to Comment

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    %d bloggers like this: