Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Created February 7, 2020 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfuenzalida/9a159fc46104687e84be25a9a8377e9a to your computer and use it in GitHub Desktop.
Save dfuenzalida/9a159fc46104687e84be25a9a8377e9a to your computer and use it in GitHub Desktop.
Solución en Clojure al Desafío de la Gran Capicuá (Programando.org)
(ns grancapicua.core)
(defn reverso [res n]
(if (zero? n)
res
(recur (+ (* 10 res) (rem n 10)) (quot n 10))))
(defn capicua? [n]
(= n (reverso 0 n)))
(defn digits [x]
(cond
(< x 10) 10
(< 10 x 100) 100
(< 100 x 1000) 1000
:else 10000))
(defn iso-fmt [y m d]
[(+ (* 1000 y) (* 100 m) d)])
(defn ddmmyyyy [y m d]
[(+ (* 10000 d) (* 100 m) y)
(+ (* (digits m) (digits y) d) (* (digits y) m) y)])
(defn mmddyyyy [y m d]
[(+ (* 10000 m) (* 100 d) y)
(+ (* (digits d) (digits y) m) (* (digits y) d) y)])
(defn capicua-date? [y m d]
(->> (mapcat #(% y m d) [iso-fmt ddmmyyyy mmddyyyy])
(some capicua?)))
(def days-month [nil 31 28 31 30 31 30 31 31 30 31 30 31])
(defn solve []
(for [y (range 1 10000)
m (range 1 13)
d (range 1 (inc (days-month m)))
:when (capicua-date? y m d)]
(iso-fmt y m d)))
;; (capicua-date? 1 1 1) ;; => true
;; (capicua-date? 1111 11 11) ;; => true
;; (capicua-date? 2020 2 2) ;; => true
;; (time (count (solve)))
;; "Elapsed time: 35227.353732 msecs"
;; 50645
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment