Day 1 of the Advent of Code
A bit late for Christmas, isn't it?
It's been a long time since I did my last post, some days of boredom have inspired me to get back at it. I am going to be trying to solve the last Christmas' Advent of Code. For those that you don't know the Advent of Code is an Advent calendar of small programming puzzles that can be solved the way you like, each day contains two puzzles in which the first is linked to the second.
If you want to get more info about it, check out this link: https://adventofcode.com/2023/about
Without further ado, let's get at it, I'm going to copy down below the statement for the Day 1
Statement |
Input |
Basically we are given a long list of characters (the one shown in the picture) where each line contains numbers and letters, we first need to get just the numbers, then store somewhere else the first and last numbers in each row, and lastly sum all those values.
A link to the input file: https://adventofcode.com/2023/day/1/input
This time the challenge is fairly straightforward (or at least it seems so once I have solved it, like it always does when it's done), I'm going to copy here the solution from my Google Collab notebook.
The result to that last step is 53194 btw, screenshot for proof.
Let's go for task number two of day 1, I guess it's going to be harder.
This one is more tricky and it actually took me a while to figure out what I was doing wrong, my first approach to transform the numbers ("one", "two", "three" and so on) into numbers was to iterate through a list that then would open a dictionary replace the value.
Well this approach was not a good one, because in instances like "eightwo" even if "eight" appears first, "two" would appear before in my list and so that it would replace it like so "eigh2". Alright, let's move on, the next approach was to use REGULAR EXPRESSIONS, it didn't work either because if in an instance like "eightwo" it would detect the "eight" in the first place it would replace it like so "8wo" and so that I would miss the second number.
At this point I was about to give up, I didn't know why it was failing, I tried in the little example that appears on top and everything to work alright, then I realized what I explained in the last paragraph, so the next strategy would be to check letter by letter until I found the match. It took me a while but I got it, here below my answer:
Comments
Post a Comment