Last active
May 30, 2023 18:33
-
-
Save ponzao/f42bc2936750dee451b52864333da95c to your computer and use it in GitHub Desktop.
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
(def numbers | |
[[" - " | |
"| |" | |
" " | |
"| |" | |
" - "] | |
[" " | |
" |" | |
" " | |
" |" | |
" "] | |
[" - " | |
" |" | |
" - " | |
"| " | |
" - "] | |
[" - " | |
" |" | |
" - " | |
" |" | |
" - "] | |
[" " | |
"| |" | |
" - " | |
" |" | |
" "] | |
[" - " | |
"| " | |
" - " | |
" |" | |
" - "] | |
[" - " | |
"| " | |
" - " | |
"| |" | |
" - "] | |
[" - " | |
" |" | |
" " | |
" |" | |
" "] | |
[" - " | |
"| |" | |
" - " | |
"| |" | |
" - "] | |
[" - " | |
"| |" | |
" - " | |
" |" | |
" - "]]) | |
(require '[clojure.string :as s]) | |
(defn- explode | |
[s ascii-number] | |
(vec | |
(mapcat (fn [row] | |
(map (fn [row] | |
(let [fst (first row) | |
lst (last row)] | |
(s/join | |
(concat | |
[fst] | |
(mapcat (fn [c] | |
(if (#{\- \space} c) | |
(repeat s c) | |
[c])) | |
(butlast (rest row))) | |
[lst])))) | |
(cond (some #{\|} row) (repeat s row) | |
:else [row]))) | |
ascii-number))) | |
(defn lcd | |
([s n] | |
(let [numbers (if (> s 1) (map (partial explode s) numbers) numbers) | |
asciis (map #(nth numbers %) | |
(map #(Character/getNumericValue %) (str n)))] | |
(s/join "\n" | |
(apply map (fn [& rows] | |
(s/join " " rows)) | |
asciis)))) | |
([n] | |
(lcd 1 n))) | |
(println | |
(lcd 5 1234567890)) | |
;= ----- ----- ----- ----- ----- ----- ----- ----- | |
;= | | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | | |
;= ----- ----- ----- ----- ----- ----- ----- | |
;= | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | |
;= | | | | | | | | | | | | | | |
;= ----- ----- ----- ----- ----- ----- ----- |
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
(require '[clojure.string :as str]) | |
(def number->ascii | |
(let [numbers (mapv | |
(fn [row] | |
(mapv str/join | |
(partition 3 row))) | |
[" _ _ _ _ _ _ _ _ " | |
"| | | _| _||_||_ |_ ||_||_|" | |
"|_| ||_ _| | _||_| ||_| _|"])] | |
(into {} | |
(map | |
(fn [m] | |
[m (map (fn [n] | |
(get-in numbers [n m])) | |
(range 0 3))]) | |
(range 0 (inc 9)))))) | |
(defn expand | |
[width height [top middle bottom]] | |
(let [variable-height (- height 3) | |
middle-height (int (/ variable-height 2)) | |
bottom-height (+ middle-height (mod variable-height 2))] | |
(map (fn [[left middle right]] | |
(str/join | |
(concat [left] | |
(repeat (- width 2) middle) | |
[right]))) | |
(concat | |
[top] | |
(conj (mapv #(str/replace % "_" " ") | |
(repeat middle-height middle)) | |
middle) | |
(conj (mapv #(str/replace % "_" " ") | |
(repeat bottom-height bottom)) | |
bottom))))) | |
(defn number-seq->ascii | |
[width height coll] | |
(let [expand-to-size (partial expand width height)] | |
(str/join "\n" | |
(apply map (fn [& rows] | |
(str/join " " rows)) | |
(map (comp expand-to-size | |
number->ascii) | |
coll))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment