-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.el
More file actions
43 lines (35 loc) · 1.22 KB
/
2.el
File metadata and controls
43 lines (35 loc) · 1.22 KB
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
37
38
39
40
41
42
43
(require 'dash)
(defun read-input (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(buffer-string)))
(defun parse-input (input-string)
(--> input-string (split-string it "\n" t)
(-map (lambda (line) (split-string line)) it)
(-tree-map #'string-to-number it)))
(defun safep (report)
(let* ((pairs (-zip-lists report (cdr report)))
(diffs (-map (-applify #'-) pairs))
(increasing (--all? (<= it 0) diffs))
(decreasing (--all? (>= it 0) diffs))
(within-limits (--all? (->> it abs ((lambda (n) (and (>= n 1) (<= n 3))))) diffs)))
;; (list increasing decreasing within-limits)))
(and (or increasing decreasing) within-limits)))
(defun relaxed-safep (report)
(let* ((n (length report))
(indices (-iota n))
(punctured (--map (-remove-at it report) indices)))
(-any? #'safep punctured)))
(setq example '((7 6 4 2 1)
(1 2 7 8 9)
(9 7 6 2 1)
(1 3 2 4 5)
(8 6 4 4 1)
(1 3 6 7 9)))
(defun part1 (input)
(-count #'safep input))
(defun part2 (input)
(-count #'relaxed-safep input))
(setq input (parse-input (read-input "2.txt")))
(part1 input)
(part2 input)