Skip to content

Instantly share code, notes, and snippets.

@Denommus
Last active December 15, 2015 23:19
Show Gist options
  • Select an option

  • Save Denommus/5339087 to your computer and use it in GitHub Desktop.

Select an option

Save Denommus/5339087 to your computer and use it in GitHub Desktop.
Solution for the Code Jam's problem of Alien Language ( https://code.google.com/codejam/contest/90101/dashboard#s=p0 ), in Common Lisp
#!/usr/bin/sbcl --script
(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))
(require 'cl-ppcre)
(with-open-file (in (car (last *posix-argv*)))
(let* ((l (read in))
(d (read in))
(n (read in))
(words (loop repeat d
collect (read-line in))))
(with-open-file (out "A.out" :direction :output :if-exists :supersede)
(loop for i from 1 to n
for test = (substitute #\] #\) (substitute #\[ #\( (read-line in))) do
(format out "Case #~D: ~D~%" i
(count test words :test #'ppcre:scan))))))
@stassats

stassats commented Apr 8, 2013

Copy link
Copy Markdown
(defun make-matcher (stream)
  (loop with match
        with matching
        for char = (read-char stream nil)
        while (and char (char/= char #\Newline #\Return))
        when (case char
               (#\(
                (setf matching t)
                nil)
               (#\)
                (let ((match (shiftf match nil)))
                  (lambda (char)
                    (member char match :test #'char=))))
               (t
                (cond (matching
                       (push char match)
                       nil)
                      (t
                       (let ((char char))
                         (lambda (x)
                           (char= x char)))))))
        collect it))

(defun test-match (matcher string)
  (every #'funcall matcher string))

(defun solve (file)
  (with-open-file (in file)
    (read in)
    (let* ((d (read in))
           (n (read in))
           (words (loop repeat d
                        collect (read-line in))))
      (loop for i from 1 to n
            for matcher = (make-matcher in)
            do
            (format t "Case #~D: ~D~%" i
                    (count matcher words :test #'test-match))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment