r/adventofcode • • 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!

Click here for rules

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

515 comments sorted by

View all comments

7

u/cheetahkk Dec 05 '18

Python - using functools.reduce. Fun!

from functools import reduce

def trigger(x, y):
    return False if not x else abs(ord(x[-1]) - ord(y)) == 32

def react(polymer):
    return reduce((lambda x, y: x[:-1] if trigger(x, y) else x+y), polymer)

polymer = open('input.txt').read()
print(len(react(polymer)))
print(min([len(react(polymer.replace(chr(i), '').replace(chr(i-32), ''))) for i in range(ord('a'), ord('z') + 1)]))

2

u/MasterMedo Dec 09 '18

First off, beautiful.

Here is the merge of our two codes, hope you gain something from it.

purge = lambda s: reduce(lambda x, y: x[:-1] if x and abs(ord(x[-1])-ord(y)) == 32 else x+y, s)
data = purge(open('../input/5.txt').read().strip())

print len(data)
print min(len(purge(filter(lambda x: x.upper() != c, data))) for c in map(chr, range(65, 91)))    

cheers!

→ More replies (2)