-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek-1-super-digit.clj
More file actions
29 lines (23 loc) · 981 Bytes
/
week-1-super-digit.clj
File metadata and controls
29 lines (23 loc) · 981 Bytes
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
; Week 1, Super Digit
(require '[clojure.string :as str])
(defn calculate-p [n k]
(let [n-range (range 0 n)]
; read-string tries to extract an object from a string,
; couldn't use Integer/parseInt because the numbers were too large
(read-string (apply str (map (fn [_] (str k)) n-range)))))
(defn integer-list [digit]
; get the individual integers by turning the number into
; a string, splitting it on '', and map them to integers
(map read-string (rest (str/split (str digit) #""))))
(defn super-digit [digit]
; if the length of the digit when turned into a string is 1,
; return that number
(if (= (count (str digit)) 1)
digit
; else, get the sum of each digit in the number
; and recurse with the sum as the new argument
(super-digit (reduce + (integer-list digit)))))
(defn super-digit-main [input]
(let [[n, k] (map read-string(str/split input #" "))]
(super-digit (calculate-p n k))))
(println (super-digit-main "148 3"))