Skip to main content

Advent of Code, day 2

We made it to Day 2 of Advent of Code

I have kept my promise for 2 days in a row.

As weird as it seems for this day and age, I have kept my promise and today is the day 2 of the Advent of Code, this time with elves involved. There you go, the statement of today's first problem down below:


And usual, my solution:

import pandas as pd
import re
import numpy as np

filepath = "/content/input 1.txt"

with open(filepath, 'r') as file:
    lines = [line.strip() for line in file.readlines()]

lines_split = []

for line in lines:
  token = re.findall(r'\w+|[^\s\w]', line)
  lines_split.append(token)

blue_list = []
green_list = []
red_list = []

for line in lines_split:
  blue,green,red = 0,0,0
  for ix, word in enumerate(line):
    if word == "blue":
      if blue < int(line[ix-1]):
        blue = int(line[ix-1])
    if word == "green":
      if green < int(line[ix-1]):
        green = int(line[ix-1])
    if word == "red":
      if red < int(line[ix-1]):
        red = int(line[ix-1])

  blue_list.append(blue)
  green_list.append(green)
  red_list.append(red)

red_max = 12
green_max = 13
blue_max = 14

index_blue = []
index_green = []
index_red = []

for ix,i in enumerate(blue_list):
  if i <= blue_max:
    index_blue.append(ix+1)

for ix,i in enumerate(green_list):
  if i <= green_max:
    index_green.append(ix+1)

for ix,i in enumerate(red_list):
  if i <= red_max:
    index_red.append(ix+1)

def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value in lst2]
    return lst3

def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value in lst2]
    return lst3

inter_1 = intersection(index_blue, index_green)
inter_2 = intersection(index_red, inter_1)

sum(inter_2)

And yes, oddly enough is the right answer:


Let's go for the second day:


Luckily enough I had already stored in three lists the minimum amount needed for each game so it's quite easy, just multiply those 3 vectors and get the results:

multiplication = []

for ix,i in enumerate(blue_list):
  res = i * green_list[ix] * red_list[ix]
  multiplication.append(res)

sum(multiplication)

And there you go, this is the result:


and a new day is finished, I already have 4 starts as you can see here:


See you tomorrow (maybe)


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

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/20

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