Where's Waldo?
You are given a vector of vectors, representing a grid. The grid is filled with values. Your job is to find a given value and return the path into the grid that will get to it.
Here's an example:
(wheres-waldo :W ;; look for :W
[[:A :B :C]
[:D :E :F]
[:G :H :I]
[:J :K :L]
[:M :N :O]
[:P :Q :R]
[:S :T :U]
[:V :W :X]
[:Y :and :Z]]);=> [7 1]
Notice that I can pass [7 1]
to get-in
to find the value.
Notes
- The nested vectors will all be the same length. This is a rectangular grid.
- If the value occurs more than once, you can return coordinates to any of them.
- Return
nil
if you can't find the value.
Bonus
Can you make it work with arbitrarily nested vectors of different sizes?
Have fun with this one! Try out different ways to implement it.
Thanks to to this site for the challenge idea!
Oh, wow, thanks!. This is super. I keep forgetting about type hinting. Most of my Clojure and ClojureScript work has been with CLJC and then most things are written in plain Clojure...
Thanks for reminding me about
keep
. Makes total sense here!I have to admit that I am still having troubles figuring
reduce
out. It is obviously a very good tool, but my brain hasn't grokked it yet. Eric's intro to Clojure where he saysmap
,filter
andreduce
are the keys to functional programming still play in my head, but while the two former are super easy for me to understand, the latter... Not so much. Maybe playing around with your input here can help me.