day5: part2 lisp

This commit is contained in:
Samuel Ortion 2024-11-28 13:57:44 +01:00
parent c1b01e4461
commit 031471c8eb
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
1 changed files with 46 additions and 0 deletions

46
2015/days/05/nice2.lisp Normal file
View File

@ -0,0 +1,46 @@
(defun twice? (string pattern)
(numberp (search pattern string
:start2 (+
(search pattern string)
(length pattern)))))
(defvar pattern)
(defun overtwice? (string)
(loop for i from 0 to (- (length string) 2)
do
(setq pattern (subseq string i (+ i 2)))
(if (twice? string pattern)
(return-from overtwice? t))
)
nil)
(defvar c1)
(defvar c2)
(defvar c3)
(defun tandem? (string)
(loop for i from 0 to (- (length string) 3)
do
(setq c1 (char string i))
(setq c2 (char string (+ i 1)))
(setq c3 (char string (+ i 2)))
(if (and (equal c1 c3) (not (equal c1 c2)))
(return-from tandem? t)))
nil)
(defun nice? (string)
(and (overtwice? string)
(tandem? string)))
(defvar my-count 0)
(with-open-file (in "./data/input")
(do ((line (read-line in nil)
(read-line in nil)))
((null line))
(if (nice? line)
(setq my-count (+ my-count 1)))
))
(print my-count)