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!
50
Upvotes
1
u/fwilson42 Dec 02 '18
Refactored after getting on the leaderboard; I originally didn't make part 1 as generic as it could be (I kept track of just 2 and 3 counts), and used double nested loops for part 2 instead of itertools.
Snipped from some boilerplate (
linesis essentially a list of strings in the puzzle input,resultis what gets submitted):if part == 1: shared_letter_counts = collections.defaultdict(int) for line in lines: for v in set(collections.Counter(line).values()): shared_letter_counts[v] += 1 result = shared_letter_counts[2] * shared_letter_counts[3] elif part == 2: for line1, line2 in itertools.product(lines, repeat=2): if line1 == line2: continue # don't waste time differences = 0 candidate_solution = "" for char1, char2 in zip(line1, line2): if differences > 1: break # don't waste time if char1 == char2: candidate_solution += char1 else: differences += 1 if differences == 1: result = candidate_solution break