(ql:quickload '(fiveam uiop)) (defconstant +num-encoding+ '((2 . #\2) (1 . #\1) (0 . #\0) (-1 . #\-) (-2 . #\=))) (defun encode-char (c) (cdr (assoc c +num-encoding+))) (defun decode-char (c) (car (rassoc c +num-encoding+))) (defun parse-snafu (num) (loop for a across (reverse num) for pow = 1 then (* pow 5) sum (* pow (decode-char a)))) (defun write-snafu (num &optional coeffs) (if (zerop num) (concatenate 'string coeffs) (multiple-value-bind (div rest) (floor num 5) (if (> rest 2) (write-snafu (1+ div) (cons (encode-char (- rest 5)) coeffs)) (write-snafu div (cons (encode-char rest) coeffs)))))) (defun solver (filename) (write-snafu (apply #'+ (mapcar #'parse-snafu (uiop:read-file-lines filename))))) (fiveam:test solutions (fiveam:is (string= "2=-1=0" (solver "eg-in"))) (fiveam:is (string= "2-212-2---=00-1--102" (solver "input"))))