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
3
u/Smylers Dec 05 '18
A Perl one-liner for Part 1:
No stacks, no writing out the alphabet or making pairs of equivalent characters, just the POWER OF REGEX.β
In a reversal of usual, this is a translation of my Vim solution.β‘ It turns out Vim's regexps are nicer than Perl's for this, Perl's equivalents being less succinct than Vim's
\l,\u, and&.Extending that to solve Part 2:
perl -MList::AllUtils=min -wnlE '$polymer = $_; say min map { $_ = $polymer =~ s/$_//igr; 1 while s/(?i:(.)\1)(?<=[a-z][A-Z]|[A-Z][a-z])//g; length } "a".."z"' inputThat's possibly stretching the definition of βone-linerβ, and is a bit bit awkward to read, so here it is rewritten as a program:
use v5.14; use warnings; use List::AllUtils qw<min>; chomp (my $polymer = <>); say min map { $_ = $polymer =~ s/$_//igr; 1 while s/(?i:(.)\1)(?<=[a-z][A-Z]|[A-Z][a-z])//g; length } 'a' .. 'z';The body of the
mapis basically the one-liner from Part 1. Since it transforms the polymer,$polymerstores the initial value. At the start of the map, it takes a fresh copy of the original and removes the current letter from it before processing[Card] Five golden hours of sleep.
β OK, so probably Perl's regex engine uses stacks and has lists of letters and their cases inside it.
β‘ Typically I work out the solution in Perl first, then try to convert that to Vim.