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!

52 Upvotes

407 comments sorted by

View all comments

1

u/scottkuma Dec 02 '18

The best way to do Advent of Code is half-blind with sleep.

Here's my Python solution for Part 2. Like much of my code, it's not elegant, but it works. The one thing that I figured out was that if the boxes varied by only one letter, you could sort the list of all boxes, and only compare neighbors in the list.

def getCommonLetters(word1, word2):
	letters = ""
	for p in range(len(word1)):
		if word1[p] == word2[p]:
			letters += word1[p]
	return letters

with open("2-input.txt") as f:
	content = f.readlines()
	
box_list = [x.strip() for x in content]
box_list.sort()

prev_box = ""
for box in box_list:
	if prev_box == "":
		prev_box = box
	else:
		cl = getCommonLetters(box,prev_box)
		if  len(box) - len(cl) == 1:
			print( getCommonLetters(box, prev_box) )
			exit()
		else:
			prev_box = box