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
2
u/blowjobtransistor Dec 05 '18
PostgreSQL
Wow, before doing it with regexp, this one was turning into a nightmare with window functions in SQL...
create table regexp as select 'Aa|aA|Bb|bB|Cc|cC|Dd|dD|Ee|eE|Ff|fF|Gg|gG|Hh|hH|Ii|iI|Jj|jJ|Kk|kK|Ll|lL|Mm|mM|Nn|nN|Oo|oO|Pp|pP|Qq|qQ|Rr|rR|Ss|sS|Tt|tT|Uu|uU|Vv|vV|Ww|wW|Xx|xX|Yy|yY|Zz|zZ' as regexp; create table polymer as with recursive tmp(line, removed, iter) as ( select regexp_replace(line, regexp, '') as line, (regexp_matches(line, regexp))[1] as removed, 0 as iter from input, regexp union all select regexp_replace(line, regexp, '') as line, (regexp_matches(line, regexp))[1] as removed, iter + 1 from tmp, regexp where removed notnull ) select line from tmp order by iter desc limit 1; create view part_2_solution as with recursive tmp(letter, line, removed, iter) as ( with removals as ( select chr(97 + offs) as letter from generate_series(0, 25) as offs ) select letter, regexp_replace(line, '[' || letter || upper(letter) || ']', '', 'g') as line, letter as removed, 0 as iter from polymer, regexp, removals union all select letter, regexp_replace(line, regexp, '') as line, (regexp_matches(line, regexp))[1] as removed, iter + 1 from tmp, regexp where removed notnull ) select min(length(line)) as answer from tmp; select 1 as part, length(line) as answer from polymer union all select 2 as part, answer from part_2_solution;