r/adventofcode • u/daggerdragon • Dec 18 '16
SOLUTION MEGATHREAD --- 2016 Day 18 Solutions ---
--- Day 18: Like a Rogue ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".
EATING YELLOW SNOW IS DEFINITELY NOT MANDATORY [?]
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!
6
Upvotes
View all comments
Show parent comment
14
u/glguy Dec 18 '16 edited Dec 18 '16
I'm going to break this down a lot because I don't want to assume much about what people know about Haskell. I apologize in advance if it's too basic or if I skip something important. Feel free to ask questions!
In Haskell the default
Stringtype is actually a type synonym for[Char]which means list of characters. I'll be taking advantage of this list structure in my solution.First I defined the rule for determining when a new cell should be a trap or a safe space. I observed that while the rule was presented in 3 parts, was not important what the middle space was going to be.
This code defines
ruleas a function of two characters with two cases. The first case is used whenxandyare equal. The second case is used if they are not.Now we'll need to define the process of computing a whole new row given the previous row.
This definition is using a list comprehension to generate the new string (and strings are just lists of characters). There's a lot to unpack here, so I'll break it down bit by bit. This definition takes a parameter
xsthat is the previous row.Extend the previous row by prefixing and suffixing an extra
'.'on either end representing the non-trap walls.Generate a list of all of the suffixes of the extended row. Example:
tails [1,2,3] == [[1,2,3],[2,3],[3],[]]. Most of these suffixes will be mapped to a new character in the next row.Here's where the list comprehension starts to come in. We match each suffix generated by tails against the pattern
x:_:y:_. This is a pattern that matches lists with at least three elements. It names the first element of that listx, ignores the second, names the thirdy, and ignores the rest of the list. In Haskell the list[1,2,3]is actually1 : 2 : 3 : []Finally, for each suffix that matches the pattern as described above we'll return a new list element whose value is
rule x y.Given that we now can generate one row from the previous we'll need to generate all the rows and count up how many safe spaces there were. We define a function that takes two parameters: the initial row of the puzzle
input, and the number of rows to considern. This function returns the number of safe spaces within the firstnrows of the generated dungeon.First, let's deal with the
$. This is just an operator that used in Haskell to save on parentheses. Instead off $ g $ h xwe could writef (g (h x)), but who has time to close all those parentheses when you're trying to be first to submit to AoC?This expression constructs a lazily (on-demand) generated list of all the rows in our dungeon.
This cuts down from all the rows of the dungeon to just the first
nrows, as requested by the problem statement.Merge all the rows into one big list of characters. We don't care which row a safe space came from, just how many safe spaces there were.
This is the section of the equality function applied to the character
'.'. It makes a new function that returnsTruewhen applied to a'.'andFalseotherwise.countis a function I defined in my helper module. Give a predicate and a list, it returns the number of elements in the list that satisfy the predicate. In this case we're checking how many safe spaces were contained in all the rows of the dungeon we're considering.The rest of the program just loads my input and prints the answer, so I won't drag everyone through it! I initially wrote this program without the type signatures and then added them after submitting for the sake of speed.