-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.lisp
46 lines (39 loc) · 1.47 KB
/
9.lisp
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
44
45
46
(ql:quickload :alexandria)
(defparameter *map* (uiop:read-file-lines "input/day9.txt"))
(defparameter *map-len* (length *map*))
(defun map-to-lists (map)
(mapcar #'(lambda (l)
(loop for i across l
collect (digit-char-p i)))
map))
(defun push-extend (item place)
(setf place (push item
(cdr (nthcdr (1- (length place))
place)))))
(defun up-and-down-boundaries (map)
(let ((ten-list (make-list 100 :initial-element 10)))
(loop for ln in map
do (push-extend 10 ln))
(push ten-list map)
(push-extend ten-list map)
(alexandria:flatten map)))
(defun low-point-p (idx old-map new-map)
(let ((n (elt old-map idx))
(left (elt new-map (1- idx)))
(right (elt new-map (1+ idx)))
(up (elt new-map (- idx 100)))
(down (elt new-map (+ idx 100))))
(every #'identity
(mapcar (lambda (x)
(< n x))
(list left right up down)))))
(defun solve-first-part (map new-map)
(loop with map-lists = (map-to-lists map)
with new-map = (up-and-down-boundaries map-lists)
for idx from 0 below (expt (length map-lists) 2)
;; for i in (alexandria:flatten map-lists)
with low-point = (low-point-p idx old-map new-map)
if (eql t low-point)
collect low-point into low-points
finally (return (length low-points)
(reduce #'+ low-points))))