We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I used (map-indexed) to associate each grid character with a column number (from 0 to 9); then I used (partition-by) to separate out sequences of + and -. I discarded the + sequences and any sequence shorter than 2. I also dropped the now superfluous - characters. Then I used (map-indexed) again to give each blank position a y coordinate as well. Then I did the same thing with the grid rotated by 90°, and so I ended up with (usually) 4 word slots represented by sequences of x,y coordinates.
Matching (e.g.) 4 words against (e.g.) 4 word slots is simple enough for a brute force approach: I matched all possible permutations of those words against the slots, punting on length mismatches. When matching words, I created a map of coordinates and contained letters, so if I encountered the same coordinate again it was a crossing and I had to ensure the new letter was the same as the one already at that location.
Printing out the completed map was simply printing, for each x,y, the character in my assignment map or a + if not found.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Crosswords-101
You are viewing a single comment's thread. Return to all comments →
This really was challenging! I spent almost 3 (partial days). Here's my solution in 80 lines of Clojure:
I used
(map-indexed)
to associate each grid character with a column number (from 0 to 9); then I used(partition-by)
to separate out sequences of+
and-
. I discarded the+
sequences and any sequence shorter than 2. I also dropped the now superfluous-
characters. Then I used(map-indexed)
again to give each blank position a y coordinate as well. Then I did the same thing with the grid rotated by 90°, and so I ended up with (usually) 4 word slots represented by sequences of x,y coordinates.Matching (e.g.) 4 words against (e.g.) 4 word slots is simple enough for a brute force approach: I matched all possible permutations of those words against the slots, punting on length mismatches. When matching words, I created a map of coordinates and contained letters, so if I encountered the same coordinate again it was a crossing and I had to ensure the new letter was the same as the one already at that location.
Printing out the completed map was simply printing, for each x,y, the character in my assignment map or a
+
if not found.