Attempt 2, day 2, a
This commit is contained in:
parent
986fe9c260
commit
9eceaf15de
44
aoc.org
44
aoc.org
|
@ -1360,3 +1360,47 @@ How many passwords are valid according to their policies?
|
|||
Hmmm, I'm too high. Besides, this has a lot of issues with cleaning up the input; I'll keep trying tomorrow. But for now, to bed!
|
||||
|
||||
Okay, I lied. I tried 740 as well -- since the last row was giving me such trouble I thought I could've got a false positive there. Oh well.
|
||||
**** Attempt 2
|
||||
|
||||
I think I've formatted the regex wrong; I also need to throw an error if there are too /many/ of the character! So the regex for =2-7 b= should look more like ... =^[^b]*b.*b.*b?.*b?.*[^b]*$=
|
||||
|
||||
#+begin_src emacs-lisp :var input=input-2
|
||||
(defun build-pw-regex (spec)
|
||||
(let* ((thing (split-string spec "[- ]"))
|
||||
(min (string-to-number (or (car thing) "0")))
|
||||
(max (string-to-number (or (cadr thing) "0")))
|
||||
(chr (caddr thing))
|
||||
(regex-begin (concat "^[^" chr "]*"))
|
||||
(regex-end (concat "[^" chr "]*$"))
|
||||
(min-regex (concat chr ".*"))
|
||||
(max-regex (concat chr "?.*"))
|
||||
(min-full-regex "")
|
||||
(max-full-regex ""))
|
||||
(dotimes (n min min-full-regex)
|
||||
(setq min-full-regex (concat min-full-regex min-regex)))
|
||||
(dotimes (x (- max min) max-full-regex)
|
||||
(setq max-full-regex (concat max-full-regex max-regex)))
|
||||
(concat regex-begin min-full-regex max-full-regex regex-end)))
|
||||
|
||||
(let* ((strlist (split-string input "\n+"))
|
||||
(alist)
|
||||
(valid-count 0))
|
||||
(dolist (line strlist alist)
|
||||
(let* ((thing (split-string line ": "))
|
||||
(regex (build-pw-regex (car thing))))
|
||||
(setq alist (cons (list regex (cadr thing)) alist))))
|
||||
;; test the regexen
|
||||
(dolist (a alist valid-count)
|
||||
(message "%s '%s' '%s'"
|
||||
(string-match (or (car a) "") (or (cadr a) ""))
|
||||
(car a) (cadr a))
|
||||
(if (string-match (or (car a) "") (or (cadr a) ""))
|
||||
(setq valid-count (1+ valid-count)))))
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
: 741
|
||||
|
||||
Well tits.
|
||||
|
||||
Okay, tomorrow (really this time!), I'm going to look for a =count= style function.
|
||||
|
|
Loading…
Reference in New Issue