AoC 2023, Day 3, Gear Ratios
Today’s puzzle is called Gear Ratios.
Part 1
I am going to start by declaring a couple of types to help me.
A digit can be a SymboolDigit or a NonSymbolDigit, where a SymbolDigit will be any digit with a symbol around it.
I also want a few helper functions. Check if a character is a symbol (not a digit or dot).
Also, need to check if the edges are symbols, so lets have a way of getting all edges for a particular point.
Also, if there is a symbol on the edge make it a SymbolDigit, or else make it a NonSymbolDigit.
I think that is the main helper functions done, So I want the main function to extract the digit groups. This uses an inner recursive function.
This will result in a list of Digit lists, so lets have a function that will convert a digit list into a PartNumber or OtherNumber.
And finally, lets convert the PartNumber into integers and ignore the OtherNumber.
Now, with the sample text, this can all be put together.
4361 is the correct answer with the sample input.
My final answer is also correct. I get another gold star, and I can move on to part 2.
Part 2
Part two will require some changes, mostly a copy paste of Part 1 answer with the required changes in each function.
I am going to create a few more types, to describe what I am working with
And then a couple of helper methods
The extract method is very similar
Similar to Part One, I now have a list of lists, so I will collect them into numbers, either with or without a gear.
The real difference starts now in matching, if two numbers have the same gear they are paired. I am going to use this function to convert to integers at the same time 1.
This can be piped together and executed
The result of 467835 is what I am expecting with the sample data.
My final answer is also correct, and I do get another star.
-
You could probably argue this violates separation of concerns (or SRP), which is a valuable rule in both functional and object oriented programming ↩︎