aboutsummaryrefslogtreecommitdiff
path: root/2022/lib.lisp
blob: 1d81219727a80441061fe46038d473612d8420a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(defun string-to-clist (str)
  (coerce str 'list))

(defun clist-to-string (clist)
  (if (atom clist)
      (string clist)
      (coerce clist 'string)))

(defun split-by-first (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-by-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-first lst delim)
            (cons first (split-by-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)))

(defun id (x) x)

(defun all (pred lst)
  (if (not (cdr lst))
      (funcall pred (car lst))
      (and (funcall pred (car lst))
         (all pred (cdr lst)))))

(defun remove-nth (n lst)
  (if (or (null lst) (= n 0))
      (cdr lst)
      (cons (car lst)
            (remove-nth
             (- n 1)
             (cdr lst)))))