Skip to content

Instantly share code, notes, and snippets.

@alandipert
Last active April 27, 2018 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/5cf98cba9b9709976a423e598097dac0 to your computer and use it in GitHub Desktop.
Save alandipert/5cf98cba9b9709976a423e598097dac0 to your computer and use it in GitHub Desktop.
;; Solution to https://uva.onlinejudge.org/external/112/p11220.pdf
(defun split-string (string &optional (sep-char #\space))
(loop for i = 0 then (1+ j)
for j = (position sep-char string :start i)
collect (subseq string i j)
while j))
(defun decode-line (str)
(loop for word in (split-string str)
with index = 0
if (> (length word) index)
collect (elt word index) into chars
and do (incf index)
finally (return (coerce chars 'string))))
(defun decode-file (file)
(with-open-file (stream file)
;; Read and discard # of cases at top of file
(read-line stream nil nil)
(loop for line = (read-line stream nil nil)
for case-sep = "" then (format nil "~%")
with case-num = 0
while line
do (if (zerop (length line))
(format t "~ACase #~A~%" case-sep (incf case-num))
(format t "~A~%" (decode-line line))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment