public
Created

  • Download Gist
gistfile1.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
(ns dojo.core
(:require
[clojure.string :as str]))
 
;;;
 
(def digits
[[" _ " "| |" "|_|"]
[" " " |" " |"]
[" _ " " _|" "|_ "]
[" _ " " _|" " _|"]
[" " "|_|" " |"]
[" _ " "|_ " " _|"]
[" _ " "|_ " "|_|"]
[" _ " " |" " |"]
[" _ " "|_|" "|_|"]
[" _ " "|_|" " _|"]])
 
(defn print-digit-string [[a b c]]
(println (str a "\n" b "\n" c)))
 
(defn print-digit [n]
(print-digit-string (nth digits n)))
 
(def digit->digit-string
(zipmap
(range 10)
(map #(nth digits %) (range 10))))
 
(def digit-string->digit
(zipmap
(vals digit->digit-string)
(keys digit->digit-string)))
 
(defn third [s]
(nth s 2))
 
(defn number->string [n]
(let [digits (->> n (format "%09d") (map str) (map read-string))
digit-strings (map digit->digit-string digits)]
(->> [(map first digit-strings)
(map second digit-strings)
(map third digit-strings)]
(interpose "\n")
(map #(apply str %))
(apply str))))
;;;
 
(defn n-partitioned-seq [partition-size seqs]
(when (every? seq seqs)
(cons
(map #(take partition-size %) seqs)
(lazy-seq
(n-partitioned-seq partition-size
(map #(drop partition-size %) seqs))))))
 
(defn digit-string-seq [s]
(let [s (n-partitioned-seq 3 s)]
(map
#(map (partial apply str) %)
s)))
 
(defn spit-file [f nums]
(->> nums
(map number->string)
(#(interleave % (repeat "\n\n")))
(apply str)
(spit f)))
 
(defn read-file [f]
(->> f
slurp
str/split-lines
(partition-all 4)
(map #(take 3 %))
(map digit-string-seq)
(map #(map digit-string->digit %))))
 
(defn digit-seq->number [s]
(->> s (apply str) read-string))
 
(defn digit-seqs->numbers [s]
(map digit-seq->digit s))
 
;;;
 
(defn checksum [n]
(let [digits (->> n str (map str) (map read-string))
total (->> digits
reverse
(map vector (range 1 10))
(map #(apply * %))
(apply +))]
(rem total 11)))
 
;;;
 
(defn digit-seq->string-and-state [s]
(let [num? (every? (complement nil?) s)
valid? (and num? (zero? (checksum (digit-seq->number s))))]
(cond
(and num? valid?)
(apply str s)
 
(and num? (not valid?))
(str (apply str s) " ERR")
 
:else
(str (apply str (map #(or % "?") s)) " ILL"))))
 
(defn digit-seqs->string-and-states [s]
(map digit-seq->string-and-state s))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.