-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.el
36 lines (31 loc) · 1 KB
/
day4.el
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
;;; -*- lexical-binding: t -*-
(defun make-pass (number)
(apply 'vector (mapcar 'string-to-number (split-string (number-to-string number) "" t))))
(defun valid-pass (number)
(defun at-least-one-duplicate (pass)
(let ((end (- (length pass) 2)))
(loop
for i from 0 to end
if (and (= (elt pass i) (elt pass (1+ i)))
;; not after
(or (= i end)
(not (= (elt pass i) (elt pass (+ i 2)))))
;; not before
(or (= i 0)
(not (= (elt pass i) (elt pass (1- i))))))
return t)))
(defun in-sequential-order (pass)
(loop
for i from 0 to (- (length pass) 2)
if (> (elt pass i) (elt pass (1+ i))) return nil
finally return t))
(let ((pass (make-pass number)))
(and (in-sequential-order pass)
(at-least-one-duplicate pass))))
(defun count-pass (low high)
(loop
for pass from low to high
count (valid-pass pass)))
(count-pass 271973 785961)
;; part2 607
;; part1 925