Skip to content

Instantly share code, notes, and snippets.

@trendsetter37
Last active December 13, 2016 16:17
Show Gist options
  • Save trendsetter37/cba1d3df583ab1181861 to your computer and use it in GitHub Desktop.
Save trendsetter37/cba1d3df583ab1181861 to your computer and use it in GitHub Desktop.
Solution to HackerRank challenge Cut the sticks in Common Lisp
;; Enter your code here. Read input from STDIN. Print output to STDOUT
(defun space-split (string)
(loop for start = 0 then (1+ finish)
for finish = (position #\Space string :start start)
collecting (parse-integer (subseq string start finish))
until (null finish)))
(defun min-from-list (list &optional (default 1000))
(reduce #'min list :initial-value default))
(defun cut-print (numbers cut-length)
;; while version is what is needed i think
(let ((x numbers) (y cut-length) (k 0))
(loop while (> (length x) 0) do
(tagbody
(setq x (map 'list (lambda (i) (- i y)) x))
(cond ((= k (length x)) (go bottom))
((not (= k (length x)))
(setq k (length x))
(format t "~d~%" k)))
bottom
(setq x (remove-if (lambda (x) (<= x 0)) x))))))
(defvar l (read))
(setq numbers (space-split (read-line)))
(setq minimum (min-from-list numbers))
(cut-print numbers minimum)
#| There are still a few kinks to work out with the printing logic |#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment