Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Created August 13, 2014 22:14
Show Gist options
  • Save dfuenzalida/09b49864f6e6b375bcb4 to your computer and use it in GitHub Desktop.
Save dfuenzalida/09b49864f6e6b375bcb4 to your computer and use it in GitHub Desktop.
(ns cross-of-zeroes.core)
;; HOWTO:
;; Create a project with:
;;
;; $ lein new cross-of-zeroes
;;
;; and replace src/cross_of_zeroes/core.clj with this file.
;;
;; Output of lein run:
;;
;; Problem 1: ([3 7])
;; Problem 2: ([4 9])
(def problem1 ["++++++++++++++++"
"++++++++++++++++"
"+++++++0++++++++"
"++++++000+++++++"
"+++++++0++++++++"
"++++++++++++++++"
"++++++++++++++++"])
(def problem2 ["++++++++++++++++++"
"++++++++++++++++++"
"+0+++++++++++000++"
"+++++++++0++++++++"
"++++++++000+++++++"
"+++++++++0++++++++"
"++++++++++++++++++"
"++++++++++++++++++"])
;; [x y] offsets around the center of the sign
(def cross-offsets
[[0 -1] [-1 0] [0 0] [1 0] [0 1]])
(defn sign-at?
"Returns true if it can find only ocurrences of the zero char in the
coordinates [x y] in the shape of the sign for the given lines"
[lines sign x y]
(let [chars (for [[i j] sign]
(-> lines (get (+ y j)) (get (+ x i))))]
(= chars
(repeat (count sign) \0)))) ; as many 0's as the sign pattern
(defn find-sign
"Loops asign the lines of text and collect the coordinates in which it finds
a sign pattern"
[lines sign]
(let [rows (count lines)
cols (count (first lines))]
(for [x (range 1 cols)
y (range 1 rows)
:when (sign-at? lines sign x y)]
[y x])))
(defn -main [& args]
(println "Problem 1:" (find-sign problem1 cross-offsets))
(println "Problem 2:" (find-sign problem2 cross-offsets)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment