Skip to main content

Advent of Code, day 1

 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.

import pandas as pd
filepath = "/content/input 1.txt"

df = pd.read_csv(filepath, header = None).iloc[:,0]

row = []
text = []
numbers = []
first_last = []

for i in df:
  for j in i:
    if j.isnumeric():
      row.append(j)
  text.append(row)
  row = []

for i in text:
  numbers.append(''.join(i))

for i in numbers:
  a = 10*int(i[0])
  b = int(i[-1])
  c = a + b
  first_last.append(c)

sum(first_last)

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:

import pandas as pd
import re

filepath_2 = "/content/input 2.txt"
input = pd.read_csv(filepath_2, header = None).iloc[:,0]

values = {
    'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
    'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9',
    'ten': '10'
}

pairs = []
for line in input:
    digits = []
    for i,c in enumerate(line):
        if line[i].isdigit():
            digits.append(line[i])
        else:
            for k in values.keys():
                if line[i:].startswith(k):
                    digits.append(values[k])
    pairs.append(int(f"{digits[0]}{digits[-1]}"))

sum(pairs)

And the result is

And finally, after being stucked for a long while in the second task, I got my second star, there you go, the proof that I did it:

Fantastic, let's see if I convince myself to do the Day 2.



Comments

Popular posts from this blog

A first approach to IoT, connecting my 3D printer to the internet

My first approach to the IoT, connecting my 3D printer to the internet IoT is one of those fancy words that people like to talk about in conferences and in TedTalks without (apparently) having too much idea of what it is all about. Set up to manage the 3D printer through the internet This one is going to be a short entry where I don't go through code or anything, just wanted to talk about a bit about how I connected my 3D printer to the internet.  I've been in the 3D printing thing for a while now, about a year and I haven't stopped printing ever since I bought my Ender 3. Fortunately enough, I live in a big house where my room/working place is on the fourth floor and my 3D printing is on the first one. You might be thinking as well: "OK Pablo but where do you want to bring us? Go to the point" Well, as you might have noticed there are two floors in betw

Computer vision to fight against Covid-19

Computer vision to fight against Covid-19 In this era technology can be very helpful in many ways, here is an example of how to reduce close contacts in the working place thanks to computer vision Welcome Manuela 15:04 (left side) and Not authorized Javier 15:05 (right side) There are many scientific publications that show how Covid-19 can survive in non-porous surfaces for some hours ( https://www.cdc.gov/coronavirus/2019-ncov/more/science-and-research/surface-transmission.html ) this might be a problem especially if those surfaces are touched by a large number of people in a short period of time. That is the case of the clock-in machine that still exist in many working places all around the world, like this one below. If not most, many of them rely on physical contact for the employee to clock-in, either it is a finger-print scan, a card that you punch-in once you arrive, or NFC devices such as cards or tokens, but not many exist that use other methods such as face recognition or