After doing days 1-12, I’ve decided I should be recording my thought process while discovering solutions to the challenges. Seeing programming personalities online use this as a way to learn a new language, I decided to write these challenges in TypeScript. I use JavaScript a lot, but it’s always been with JQuery, so TypeScript isn’t new per-say and I’m not finding it that challenging. Next year will be different!
Summary
The core of this challenge is to find the “reflection” hidden in the pattern. Reflections can be vertical or horizontal. Add all the lines to the left of vertical reflections and add them to 100 multiplied by all the lines above horizontal reflections.
Example of a pattern:
Illustrated:
Full challenge: Day 13
Horizontal Reflection Points
My original thinking was to break up this pattern into a nested array
string[][]
, loop through each cell and checking the indexes that could
possibly be reflections. Something like:
|
|
I was thinking I could even check the vertical reflection at the same time as horizontal.
Upon writing this loop and realizing I’d have to write edge cases I decided to try doing a straight comparison of rows:
|
|
Which then turned into:
|
|
Realizing this is the path of least resistance, I ditched the nested array,
string[][]
, idea and decided to keep it in the same format I read it in as;
a normal array, string[]
.
My new reflection detection looked like this:
|
|
I then decided, “Hey, I could use a map + filter instead!”
Vertical Reflection Points
With my wonderful, possibly not the most efficient but nice looking, map + filter
combination I started to dread redoing this to check verticals reflections. My
solution is to “rotate” the whole puzzle clockwise and feed it into the
same get_horizontal_reflections
function.
Note: If we rotated it counter clockwise than the “lines to the left of vertical reflections” would be at the bottom, clockwise they’re at the top the exact same number we need for the horizontal- less work!
|
|
Reflection Point Validation
Now that, my newly renamed, get_reflection_points()
function can find both the
horizontal and vertical->horizontal points, I need to make sure that this is
actually correct.
My observations to get this done are:
- if the point is 3 then the actual reflection is between 3 and 4.
- if the point is 3 then there are 3 rows above the reflection point
- if we take the length of the pattern and subtract 2, we get the rows below the reflection point.
Using that my validation looks like this:
|
|
Wrapping it Together
As noted above if the index is 3 then there are 3 rows above the
reflection point, which is the number we need to find the answer.
get_reflection_points()
returns the index of all valid reflection points as a
number array, number[]
, but it always seems to be 1 or 0.
A bare bones version of putting this together would look like this:
|
|
This is generally my solution. My actual version is slightly different as it uses interfaces to store puzzles with a puzzle number, has more validation and opportunities for meaningful debugging statements.