~rauhl/advent-of-code

2f6a096d1f78a702763bcc2a80de6db9b9d0bd30 — Robert A. Uhl 10 months ago 955b25b
Day 4
1 files changed, 29 insertions(+), 0 deletions(-)

A 2019-12-04.lisp
A 2019-12-04.lisp => 2019-12-04.lisp +29 -0
@@ 0,0 1,29 @@
(defpackage #:aoc/2019/4 (:use #:cl))
(in-package #:aoc/2019/4)

(ql:quickload "cl-ppcre")


(defun candidatep-1 (n &aux (nn (format nil "~d" n)))
  (and (equal (sort (copy-seq nn) #'char<) nn)
       (cl-ppcre:all-matches-as-strings "(\\d)\\1+" nn)))

(defun candidate-passwords-1 (start end)
  (loop for n from start to end
	when (candidatep-1 n)
	  collect n))

(defun part1 ()
  (length (candidate-passwords-1 125730 579381)))

(defun candidatep-2 (n &aux (nn (format nil "~d" n)))
  (and (equal (sort (copy-seq nn) #'char<) nn)
       (find 2 (cl-ppcre:all-matches-as-strings "(\\d)\\1+" nn) :key #'length)))

(defun candidate-passwords-2 (start end)
  (loop for n from start to end
	when (candidatep-1 n)
	  collect n))

(defun part2 ()
  (length (candidate-passwords-2 125730 579381)))