diff options
-rw-r--r-- | 2022/lib.lisp | 28 | ||||
-rw-r--r-- | 2022/puzzle-2.lisp | 3 | ||||
-rw-r--r-- | 2022/puzzle-3.lisp | 4 |
3 files changed, 31 insertions, 4 deletions
diff --git a/2022/lib.lisp b/2022/lib.lisp new file mode 100644 index 0000000..b6fa70f --- /dev/null +++ b/2022/lib.lisp @@ -0,0 +1,28 @@ +(defun string-to-clist (str) + (coerce str 'list)) + +(defun clist-to-string (clist) + (if (atom clist) + (string clist) + (coerce clist 'string))) + +(defun split-by (lst delim) + "Splits LST by the first instance of DELIM" + (let ((pos (position delim lst))) + (if pos + (cons (subseq lst 0 pos) (list (subseq lst (+ pos 1)))) + (error (format nil "No instance of ~a was found in ~a" delim lst))))) + +(defun split-completely (lst delim) + (if (or (null lst) (not (cdr lst))) + (cons (list (car lst)) nil) + (if (member delim lst) + (destructuring-bind (first rest) (split-by lst delim) + (cons first (split-completely rest delim))) + (list lst)))) + +(defun get-lines (input-string) + (with-input-from-string (s input-string) + (loop for line = (read-line s nil) + while line + collect line))) diff --git a/2022/puzzle-2.lisp b/2022/puzzle-2.lisp index a17fdb2..d91795a 100644 --- a/2022/puzzle-2.lisp +++ b/2022/puzzle-2.lisp @@ -16,7 +16,8 @@ collect (let ((opponent (subseq strategy 0 1)) (yours (subseq strategy 2 3))) - (list (sensible-convert-input opponent) (sensible-convert-input yours)))))) + (list (sensible-convert-input opponent) + (sensible-convert-input yours)))))) (loop for round in rounds diff --git a/2022/puzzle-3.lisp b/2022/puzzle-3.lisp index e2e17cc..56a31b6 100644 --- a/2022/puzzle-3.lisp +++ b/2022/puzzle-3.lisp @@ -1,3 +1,4 @@ +(load "lib.lisp") (defvar input (uiop:read-file-string "3-input")) (defun split-string-in-two (s) @@ -10,9 +11,6 @@ until (null line) collect (split-string-in-two line)))) -(defun string-to-clist (str) - (loop for char across str collect char)) - (defun common-types (s1 s2) (car (intersection (string-to-clist s1) |