aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-06-27 21:00:39 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-17 14:14:38 +0100
commit2013cb4f5fbb2aae4d4e7c15491f7aa4f8fcb1a1 (patch)
tree978d9e85cd3199890d1cb50214da55d8c988a52f
parent1041984da04df8d2968e3b45fd70beedd7b3ba8b (diff)
downloadadvent-of-code-2013cb4f5fbb2aae4d4e7c15491f7aa4f8fcb1a1.tar.gz
advent-of-code-2013cb4f5fbb2aae4d4e7c15491f7aa4f8fcb1a1.tar.bz2
advent-of-code-2013cb4f5fbb2aae4d4e7c15491f7aa4f8fcb1a1.zip
(2022>6)+solution for problem 6
-rw-r--r--2022/puzzle-6.lisp47
1 files changed, 47 insertions, 0 deletions
diff --git a/2022/puzzle-6.lisp b/2022/puzzle-6.lisp
new file mode 100644
index 0000000..b3fa458
--- /dev/null
+++ b/2022/puzzle-6.lisp
@@ -0,0 +1,47 @@
+(defvar input (with-input-from-string (s (uiop:read-file-string "2022/6-input"))
+ (read-line s nil)))
+
+(defun is-unique (lst)
+ (or (null lst)
+ (and (not (member (car lst) (cdr lst)))
+ (is-unique (cdr lst)))))
+
+(defun string-to-list (str)
+ (loop for char across str collect char))
+
+(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-list 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-list 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))