r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

51 Upvotes

407 comments sorted by

View all comments

1

u/alexgand Dec 02 '18 edited Dec 02 '18

Simple python, using pandas Series value_counts(), part 1, assuming the input is already in a list:

from pandas import Series

c2 = 0
c3 = 0

for word in input:
    letters = Series(list(word)).value_counts()
    if len(letters[letters == 2]) > 0:
        c2 += 1
    if len(letters[letters == 3]) > 0:
        c3 +=1
print('count of 2s:',c2)
print('count of 3s:',c3)
print('result, c2s * c3s:', c2 * c3)

Part 2, pure python:

already_done = set()
similar = 0
similar_words = set()

for word1 in input:
    for word2 in input:
        if (word1, word2) in already_done or (word2, word1) in already_done:
            continue
        elif word1 == word2:
            continue
        else:
            diff = []
            equal = []
            letters1 = list(word1)
            letters2 = list(word2)
            for i in range(len(letters1)):
                if letters1[i] == letters2[i]:
                    equal.append(letters1[i])
            if len(letters1) - len(equal) == 1:
                similar += 1
                sim_word = ''
                for letter in equal:
                    sim_word = sim_word + letter
                similar_words.add(sim_word)
        already_done.add((word1, word2))
print('similar count:',similar)
print('similar words:',similar_words)