advent of code day 3: i might be r-slurred edition

idk how to do multi line spoilers lol



letters = ['','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

total_score = 0

team = []

for line in open("input.txt"):
    team.append(line.strip())

for i in range(2, len(team), 3):
    for letter in team[i]:
        if letter in team[i-2] and letter in team[i-1]:
            total_score += letters.index(letter)
            break

print(total_score)

some real caveman shit but it works

80
Jump in the discussion.

No email address required.

It's not usually this bad, this is just me being r-slurred

Jump in the discussion.

No email address required.

yeah man, maybe u should have just used a lookup array? tho, idk wtf ur doing really, so yeah.

Jump in the discussion.

No email address required.

For every 3 lines:

  1. one by one split them in half, put each half's characters in a set, find the set intersection i.e. the character that's present in both halves and get its value

  2. take all 3 lines and find the character that's present in all 3 also using sets and get its value

  3. the values get summed up in their respective variables, sum and sum2

It's a bit difficult if you don't know functional programming.

Also cleaned it up a bit:

![](/images/16700975227873366.webp)

Jump in the discussion.

No email address required.

fuk man, i wrote my solution in about 10 lines using js split and reduce, with one hashmap. if that's not "functional", then idc for whatever functional is.

but what ur doing is ... u don't need to put the 2nd half in another hashmap just so u can use builtins to find the intersection between the two, lol. u can just iterate over the 2nd half and as soon as u find a character in the hashmap of the 1st half .... that's ur "intersetion". i suppose the built-ins do that for u, but it doesn't look easier to me, and ur creating excess data structures.

but (2) is even more mind boggling. once u have the set intersection, that should be one character, and why do u ever need to make sure it's present in all 3?

Jump in the discussion.

No email address required.

u can just iterate over the 2nd half and as soon as u find a character in the hashmap of the 1st half .... that's ur "intersetion".

Well yeah, I could've but at this point it's a difference of a couple of lines at most so w/e.

and why do u ever need to make sure it's present in all 3?

Because that's the point of the puzzle?

In their example 'r' was the solution but if I did what you say, I'd also get 'f'

![](/images/16701063296668978.webp)

Jump in the discussion.

No email address required.

if only the problem was a couple extra lines.

and the insection itself guarantees it's in both sets, already. are r u doing additional searches to find something already known?

Jump in the discussion.

No email address required.

I'm not doing any additional searches, after the first intersection, the resulting set only has a few characters. That set is then intersected with the third one. It's not like I'm doing a triple for loop.

Jump in the discussion.

No email address required.

the 1st and 2nd half need to be put into sets and intersected, and that intersection should be a set with 1 character. where is this 2nd intersection even coming from?

Jump in the discussion.

No email address required.

ok, i'm a dumbass and just had no idea about the second part. lol. still have no idea what ur code is doing. like what r backpacks?

Jump in the discussion.

No email address required.

2 intersections are for part 2 of the problem. Part 1 is 1 intersection obviously. I don't get what you're confused by.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.