Repository navigation
Expand file tree
/
Copy pathday11.lisp
More file actions
49 lines (40 loc) · 1.91 KB
/
Copy pathday11.lisp
File metadata and controls
49 lines (40 loc) · 1.91 KB
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
47
48
49
(defpackage :aoc/2015/11 #.cl-user::*aoc-use*)
(in-package :aoc/2015/11)
(defun next-char (ch)
(if (char= ch #\z) #\a (code-char (1+ (char-code ch)))))
(defun next-password (password)
(labels ((recur (password &aux
(ch (first password)) (ch-next (next-char ch)))
(cond ((char/= ch-next #\a) (cons ch-next (rest password)))
(t (cons ch-next (recur (rest password)))))))
(recur password)))
(defun three-decreasing-straight-p (password)
(loop for ch1 in password for code1 = (char-code ch1)
for ch2 in (cdr password) for code2 = (char-code ch2)
for ch3 in (cddr password) for code3 = (char-code ch3)
thereis (= code1 (1+ code2) (+ code3 2))))
(defun not-confusing-p (password)
(not (some (lambda (ch) (find ch "ilo" :test #'char=)) password)))
(defun two-different-non-overlapping-pairs-p (password)
(loop for (ch1 ch2 . rest) on password
thereis (and (eql ch1 ch2)
(loop for (ch3 ch4 ch5) on rest
thereis (and (not (eql ch2 ch3))
(eql ch3 ch4)
(not (eql ch4 ch5)))))))
(defun password-valid-p (password)
(and (three-decreasing-straight-p password)
(not-confusing-p password)
(two-different-non-overlapping-pairs-p password)))
(defun new-password (string)
(flet ((string-to-password (string)
(reverse (mapcar #'parse-char (cl-ppcre:split "" string))))
(password-to-string (password)
(format nil "~{~A~}" (reverse password))))
(loop with curr = (string-to-password string)
do (setf curr (next-password curr))
when (password-valid-p curr) return (password-to-string curr))))
(define-solution (2015 11) (password first)
(let ((part1 (new-password password)))
(values part1 (new-password part1))))
(define-test (2015 11) ("vzbxxyzz" "vzcaabcc"))