• + 0 comments

    I tried doing this with common-lisp and realised there no string spliting function in standard lib lol.

    ;; SOLUTION
    (defun super-digit (int-str)
      "Parse each char of INT-STR as int then sum and stringify, repeat until single char."
      (if (= (length int-str) 1)
          (parse-integer int-str)
          ;; tail
          (super-digit
           ;; int-str
           (prin1-to-string
            (apply '+
                   (mapcar
                    (lambda (x) (- (char-int x) 48)) ; 48 is char code of 0
                    (coerce int-str 'list)))))))
    
    ;; this helps parsing stdin
    (defun split-string (str delim)
      (let ((acc '())
            (beg 0))
        (loop while (let ((end (position delim str :start beg)))
                      (progn
                        (push (subseq str beg end) acc)
                        (if end
                            (setq beg (+ 1 end))
                            nil ; break while
                            ))))
        (nreverse acc)))
    
    ;; MAIN
    (let* ((pair-n-k (split-string (read-line) #\Space))
           (n (car pair-n-k))
           (k (parse-integer (cadr pair-n-k))))
      (write (super-digit (prin1-to-string (* k (super-digit n))))))