Advent-of-Code/day6.lisp

98 lines
3.0 KiB
Common Lisp

(defparameter *test-input-1* "mjqjpqmgbljsphdztnvjfqwrcgsmlb")
(defparameter *test-input-2* "bvwbjplbgvbhsrlpgdmjqwftvncz")
(defparameter *test-input-3* "nppdvjthqldpwncqszvftbrmjlhg")
(defparameter *test-input-4* "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg")
(defparameter *test-input-5* "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw")
(defun all-char-different-p (str)
)
;; (set-difference (list 1 2 3 1 1) (list 3))
(ql:quickload "fset")
(defparameter *test-set* (fset:empty-set))
(fset:with *test-set* 4)
(fset:set 'a 'ab 'c)
(fset:set '(a b c ))
(apply #'fset:set '(a b c))
(eval (macroexpand `(fset:set ,@'(1 2 3))))
(fset:size (coerce "hello" 'list))
(subseq (coerce *test-input-1* 'list) 0 4)
(let
((str *test-input-5*))
(do*
((line (coerce str 'list) (cdr line))
(checked-suf (subseq line 0 4) (subseq line 0 4))
(suf-set (eval (macroexpand `(fset:set ,@(coerce checked-suf 'list)))) (eval (macroexpand `(fset:set ,@(coerce checked-suf 'list)))))
(ind 4 (1+ ind)))
((= 4 (fset:size suf-set)) ind)
(format t "another step with ~A~%" checked-suf)))
(subseq '(a b c d s e t d) 0 5)
(defun get-index-after-first-quad-unique (str)
(do*
((line (coerce str 'list)
(cdr line))
(checked-suf (subseq line 0 4)
(subseq line 0 4))
(suf-set (eval (macroexpand `(fset:set ,@(coerce checked-suf 'list))))
(eval (macroexpand `(fset:set ,@(coerce checked-suf 'list)))))
(ind 4 (1+ ind)))
((= 4 (fset:size suf-set))
ind)))
(with-open-file (in "day6-input.txt")
(let ((line (read-line in)))
(get-index-after-first-quad-unique line)))
(defun get-index-after-first-14-unique (str)
(do*
((line (coerce str 'list)
(cdr line))
(checked-suf (subseq line 0 14)
(subseq line 0 14))
(suf-set (eval (macroexpand `(fset:set ,@(coerce checked-suf 'list))))
(eval (macroexpand `(fset:set ,@(coerce checked-suf 'list)))))
(ind 14 (1+ ind)))
((= 14 (fset:size suf-set))
ind)))
(get-index-after-first-14-unique *test-input-1*)
(get-index-after-first-14-unique *test-input-2*)
(get-index-after-first-14-unique *test-input-3*)
(get-index-after-first-14-unique *test-input-4*)
(get-index-after-first-14-unique *test-input-5*)
(with-open-file (in "day6-input.txt")
(let ((line (read-line in)))
(get-index-after-first-14-unique line)))
;;; but in Scala it would be something like this:
;; "mjqjpqmgbljsphdztnvjfqwrcgsmlb".toList
;; .sliding(4)
;; .map(_.toSet)
;; .zipWithIndex
;; .find(_._1.size == 4)
;; .map(_._2 + 4)
;; def findIndexAfterNUnique(str: String, n: Int): Option[Int] = {
;; str.toList
;; .sliding(n)
;; .map(_.toSet)
;; .zipWithIndex
;; .find(_._1.size == n)
;; .map(_._2 + n)
;; }
;; findIndexAfterNUnique("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 4)
;; findIndexAfterNUnique("bvwbjplbgvbhsrlpgdmjqwftvncz", 4)
;; findIndexAfterNUnique("nppdvjthqldpwncqszvftbrmjlhg", 4)
;; findIndexAfterNUnique("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 4)
;; findIndexAfterNUnique("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 4)