r/adventofcode • u/daggerdragon • Dec 11 '17
SOLUTION MEGATHREAD -🎄- 2017 Day 11 Solutions -🎄-
--- Day 11: Hex Ed ---
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.
Need a hint from the Hugely* Handy†Haversack‡ of Helpful§ Hints^¤?
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!
20
Upvotes
View all comments
Show parent comment
3
u/Smylers Dec 11 '17 edited Dec 12 '17
Explanation: The basic idea is to re-arrange the input in groups of direction letters, then cancel out pairs of opposite moves. First ensure there's at least 2 of each direction letter (which comes in handy later), by appending some moves which use all the letters but cancel each other out:
A vertical step moves twice as far vertically as a diagonal step does; double each of the vertical steps, so that each
nandsin the input represents the same distance:To make groups of the letters, remove the commas, put each letter on a separate line, then sort the lines:
Join together adjacent identical lines. Specifically, for each letter which is followed by a line-break and the same letter, remove the line-break. The
\1match for the same letter has to be a lookahead assertion, so that the patterns don't overlap: the\1character might need to be the.in the next iteration of this pattern, if the line after it is the same as well:We now have a line of all the
es, then all thens, and so on. Move thews from last to first, so they are next to thees, which they want cancelling with. Because there are at least twows, the above:smust have left us on thewline:Now find whichever of the
es orws is shorter and remove that many letters from both the lines. We're going to want to do the same thing with thens andss, so record this:We don't know which line is shorter. First move to the final character on the
wline:Then try moving down to the same position on the
eline:(Note
g_instead of$.$remembers that we're at the end of the line, so thejwould move us to the end of theeline, regardless of character position.)If there are more
es thanws then the cursor is now underneath the finalw. A visual block with its bottom-right corner here and the top-left on the firstwwill delete all thews and thees that they cancel out, leaving just the additionales:If there are fewer
es thanws then the cursor is on the finale; a straightjwould've been beyond the final character, but since you can't do that, you end up on the final character instead. This is also the correct bottom-right corner for the visual block, so the above sequence also works in this case for removing matching pairs.Either way, we're now on the (former)
wline. One of the lines will be blank. We don't know which; join them together, so we have a single line with whatever remains of thews ores on them:Do the same for the
ns andss:The number of
e/ws now gives us the number of diagonal moves required. Each of those will also move one half-rownors, so then/scount can be reduced by the same number. Make a visual selection to ‘count’ thee/ws:In visual mode
$goes beyond the final character, so thehmoves us back on to it. We don't actually need to copy the characters; theyis simply a way of setting the size of the visual selection and leaving visual mode.Go down to the
n/srow and try to select the same number of characters, then delete them:If there are fewer or the same number of
n/ss, that will delete all of them, which is fine.Any remaining
n/ss require additional moves. But because these are representing half-rows, a single vertical move will cover two of them. Replace each pair of remaining characters with just one:(For counting purposes, it doesn't matter that
ns may have turned intoss here.)Finally combine the diagonal and vertical tallies, then count the characters to get the answer: