Skip to content

Instantly share code, notes, and snippets.

@tdrhq
Created December 24, 2021 06:43
Show Gist options
  • Select an option

  • Save tdrhq/74a1c9100851066d1363d89872bbd4e1 to your computer and use it in GitHub Desktop.

Select an option

Save tdrhq/74a1c9100851066d1363d89872bbd4e1 to your computer and use it in GitHub Desktop.

Revisions

  1. tdrhq created this gist Dec 24, 2021.
    70 changes: 70 additions & 0 deletions foo.lisp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    (defun decompile (lines)
    (let ((lines (mapcar (lambda (x)
    (read-from-string (format nil "(~a)" x)))
    lines)))
    (let ((expr `(when (= z 0)
    (return-from do-run choices)))
    (inp 0))
    (loop for line in (reverse lines) do
    (destructuring-bind (cmd a &optional b) line
    (let ((decl `(declare (type fixnum ,a))))
    (setf expr
    (ecase cmd
    (add `(let ((,a (+ ,a ,b)))
    ,decl
    ,expr))
    (mul `(let ((,a (* ,a ,b)))
    ,decl
    ,expr))
    (eql `(let ((,a (if (eql ,a ,b) 1 0))) ,decl ,expr))
    (div `(let ((,a (floor ,a ,b))) ,decl ,expr))
    (mod `(let ((,a (mod ,a ,b))) ,decl ,expr))
    (inp
    (incf inp)
    `(with-input (lambda (,a) (declare (optimize (speed 3)))
    ,decl
    #+nil
    ,(when (= 7 inp)
    `(log:info "Current values: ~s, ~s, ~s" x y z))
    (symbol-macrolet ((key
    (gethash (list ,inp
    x y z
    ,a)
    cache)))
    (or key
    (setf key
    ,expr)))))))))
    (eval `(defun do-run ()
    (declare (optimize (speed 3) (safety 0) (debug 0)))
    (let ((choices nil)
    (cache (make-hash-table :test 'equal))
    (len 0))
    (declare (type fixnum divisor))
    (let ((x 0) (y 0) (w 0) (z 0))
    (flet ((with-input (fn)
    (push nil choices)
    (incf len)
    (when (= 7 len)
    (log:info "looking at: ~s" (cdr choices) ))

    (loop for i from 1 to 9 do
    (progn
    (setf (car choices) i)
    (funcall fn i)))
    (decf len)
    (pop choices)))
    ,expr)))))
    (compile 'do-run)
    )))))

    (do-run)

    (loop for i from (expt 9 14) 0
    for actual = (let ((i i))
    (loop while )))


    (test simple
    (decompile (uiop:read-file-lines "~/builds/web/input"))

    (pass))