src/2017/days/day-04.lisp @ dd27f6b2d778

2021/05 and 2021/06
author Steve Losh <steve@stevelosh.com>
date Mon, 06 Dec 2021 19:38:12 -0800
parents 182bdd87fd9e
children (none)
(advent:defpackage* :advent/2017/04)
(in-package :advent/2017/04)

(defun valid-hash-table-test-p (test)
  (member test `(eq eql equal equalp ,#'eq ,#'eql ,#'equal ,#'equalp)))

(defun ensure-boolean (value)
  (if value t nil))

(defun contains-duplicates-p (sequence &key (test #'eql))
  (ensure-boolean (if (valid-hash-table-test-p test)
        (iterate
          (with seen = (make-hash-set :test test))
          (for value :in-whatever sequence)
          (thereis (hset-contains-p seen value))
          (hset-insert! seen value))
        (etypecase sequence
          (list (iterate
                  (for (value . remaining) :on sequence)
                  (thereis (position value remaining :test test))))
          (sequence
            (iterate
              (for i :from 0 :below (length sequence))
              (for value = (elt sequence i))
              (thereis (position value sequence :start (1+ i) :test test))))))))


(defun anagramp (string1 string2)
  (string= (sort (copy-seq string1) #'char<)
           (sort (copy-seq string2) #'char<)))

(define-problem (2017 4) (data read-lines-of-words) (337 231)
  (values (count-if (lambda (phrase)
                      (not (contains-duplicates-p phrase :test #'equal)))
                    data)
          (count-if (lambda (phrase)
                      (not (contains-duplicates-p phrase :test #'anagramp)))
                    data)))