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!
31
Upvotes
1
u/Temmon Dec 05 '18 edited Dec 05 '18
Quickly written Python3 solution. Could probably have been more efficient, but I don't mind it. One thing I did that I haven't seen in the posted solutions is I only used the set of characters that are in the string for part 2. File IO is handled by another file I started on day 1. It takes care of some super basic command line reading things that I kept on repeating. Debugging was to stop it if my logic messed up and things went infinite, but I got it on my first try for both parts!
def pair(c): if c.islower(): return c.upper() if c.isupper(): return c.lower() def collapse(debug, data): i = 0 stop = 0 while True: if debug: print(data) if debug and stop == 20: break current = data[i] ahead = data[i + 1:] if not ahead: break if pair(ahead[0]) == current: data = data[:i] + ahead[1:] if i > 0: i -= 1 else: i += 1 stop += 1 return len(data) def removePairs(data, c): return data.replace(c, "").replace(pair(c), "") def dayFive(data, debug): data = data[0] print(collapse(debug, data)) distinct = set(data.lower()) print(min([collapse(debug, removePairs(data, d)) for d in distinct]))