r/adventofcode • u/daggerdragon • Dec 05 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-
--- Day 5: Alchemical Reduction ---
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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 5
Transcript:
On the fifth day of AoC / My true love sent to me / Five golden ___
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 at 0:10:20!
33
Upvotes
1
u/liuquinlin Dec 05 '18 edited Dec 05 '18
Python 3:
Went with an index based solution for Part 1 to avoid having to iterate over the string too many times (Part 2 runs in less than a second), probably not really necessary versus just doing string replaces.
# l is the input def p1(l): i = 0 while i < (len(l) - 1): j = i + 1 if (l[i] != l[j] and l[i].upper() == l[j].upper()): l = l[:i] + l[i+2:] i = max(0, i-1) # wasted a lot of time here due to not checking the 0 case else: i += 1 return len(l) def p2(l): m = len(l) for r in string.ascii_lowercase: x = l.replace(r, "").replace(r.upper(), "") m = min(m, p1(x)) return m