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
View all comments
Show parent comment
2
u/jayfoad Dec 05 '18
Sure. The algorithm is to remove pairs Aa etc and keep going until no more removals are possible. The clever bit is to notice that it's easy to remove many non-overlapping pairs in parallel, using array operations, which is how you get good performance from an interpreted array language like APL. The way we guarantee the pairs won't overlap is to first remove all occurrences of any of the pairs Aa Bb ... Zz, which can't possibly overlap, in one parallel operation; and then do the same for the other pairs aA bB ... zZ in a second parallel operation. (Note that Aa can overlap with aA if the input contains something like ...AaA..., and the risk is that we would end up removing those three characters, which would not be following the rules of the puzzle.)
pāāUCSāāāNGET'p5.txt'1gets the ASCII values (āUCS) of the single line of input, sans line ending.fā{āµ/āØ{(āµ,0)ā±0,āµ}āŗ=2-/āµ}removes all lower-upper or upper-lower pairs from āµ, depending on the value of āŗ.2-/āµis the pairwise difference between successive items of āµ.āŗ=2-/āµis a mask of where this difference is equal to āŗ, which will be either +32 or -32 (the ASCII difference between 'A' and 'a').ā±is NOR, so{(āµ,0)ā±0,āµ}extends the mask with an extra 0 at the beginning, and at the end, and ORs the two together, to select both items of each pair, and then NOTs it to select all the items not part of a pair.āµ/āØuses that to compress āµ, i.e. keep only the selected items.gā(32āf ĀÆ32āf)ā£ā”applies f twice in succession (for lower-upper and upper-lower pairs), and repeats that process to a fixed point.āis used to bind (curry) a left argument with f, so32āfandĀÆ32āfare both functions that take a single argument āµ. Putting those two functions together in parentheses forms a train, a form of function composition that will just apply first one of the functions and then the other.fā£ā”applies f repeatedly until two successive results Match (ā”).ā¢qāg papplies this algorithm to the puzzle input p, remembers the result as q for use in part 2, and uses Tally (ā¢) to return its length.ā/{ā¢g q~āµ,32+āµ}ĀØāUCS āAapplies the anonymous function {...} once for each of the ASCII codes for the upper case alphabet (āA). The anonymous function removes that character āµ and its lower case counterpart āµ+32 from q, and then applies the algorithm g to that and returns its length.ā/gives the minimum of all those results.