advent-of-code-2022/day06/main.lisp

27 lines
820 B
Common Lisp
Raw Permalink Normal View History

2022-12-06 09:50:43 +01:00
2022-12-06 10:18:45 +01:00
; Split to list, remove duplicates, recombine
2022-12-06 09:50:43 +01:00
(defun unique-string (str)
2022-12-06 10:18:45 +01:00
(concatenate 'string (remove-duplicates (coerce str 'list))))
2022-12-06 09:50:43 +01:00
2022-12-06 10:18:45 +01:00
; Returns the index of the first occurrence of a sequence of "len" different characters in "str"
(defun unique-sequence (str len &optional (offset 0))
(if (= len
(length (unique-string (subseq str offset (+ len offset)))))
offset
(unique-sequence str len (+ offset 1))))
2022-12-06 09:50:43 +01:00
2022-12-06 10:18:45 +01:00
; unique-sequence gives the index-of, but we want the index-after
2022-12-06 09:50:43 +01:00
(defun part1 (str)
2022-12-06 10:18:45 +01:00
(+ 4 (unique-sequence str 4)))
(defun part2 (str)
(+ 14 (unique-sequence str 14)))
2022-12-06 09:50:43 +01:00
(let ((in (open "input.txt" :if-does-not-exist nil)))
(when in
2022-12-06 10:18:45 +01:00
(let ((str (read-line in nil nil)))
(format t "Part 1: ~a~%" (part1 str))
(format t "Part 2: ~a~%" (part2 str)))
2022-12-06 09:50:43 +01:00
(close in)))