Solución en Clojure al Desafío de la Gran Capicuá (Programando.org)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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