aboutsummaryrefslogtreecommitdiff
path: root/2022/puzzle-6.lisp
blob: 7aed004c99e11b0e14fcb0a6bda721d371d521c7 (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
45
46
(load "lib.lisp")

(defvar input (with-input-from-string (s (uiop:read-file-string "6-input"))
                (read-line s nil)))

(defun is-unique (lst)
  "If LST has no duplicates, returns true."
  (or (null lst)
     (and (not (member (car lst) (cdr lst)))
        (is-unique (cdr lst)))))

(defun first-round (input)
  (defun calculate-position (nth)
    "Calculate the actual position of the nth item."
    (if nth
        (+ nth 4)
        nil))

  (let* ((char-list (string-to-clist input))
         (unique-check (mapcar (lambda (x y z w) (is-unique (list x y z w))) char-list (cdr char-list) (cdr (cdr char-list)) (cdr (cdr (cdr char-list))))))
    (calculate-position (position t unique-check))))

(format t "First round: ~a~%" (first-round input))

(defun successive-by (lst n)
  "Returns consecutive subsequences of size N in LST."
  (loop
    for item in lst
    for i from 0
    collect
    (if (> (+ n i) (length lst))
        nil
        (subseq lst i (+ n i)))))

(defun second-round (input)
  (defun calculate-position (nth)
    "Calculate the actual position of the nth item."
    (if nth
        (+ nth 14)
        nil))

  (let* ((char-list (string-to-clist input))
         (unique-check (mapcar #'is-unique (successive-by char-list 14))))
    (calculate-position (position t unique-check))))

(format t "Second round: ~a~%" (second-round input))