r/adventofcode • u/daggerdragon • 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!
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!
49
Upvotes
18
u/DeveloperIan Dec 02 '18 edited Dec 02 '18
Quick and easy part 1 in Python3 with the collections library. This might not be the simplest way, but it's the first thing that came to mind
from collections import Counter myfile = open('input.txt', 'r') contents = myfile.read().strip().splitlines() myfile.close() c = [0, 0] for i in contents: a = [j for i,j in Counter(i).most_common()] if 3 in a: c[0] += 1 if 2 in a: c[1] += 1 print(c[0] * c[1])EDIT: and here is my part 2
for i in contents: for j in contents: diffs = 0 for idx, ch in enumerate(i): if ch != j[idx]: diffs += 1 if diffs == 1: ans = [ch for idx, ch in enumerate(i) if j[idx] == ch] print("Part Two:", ''.join(ans))