master
4903000 4 years ago
parent 8c5d9fe59c
commit 8a2465757c

@ -73,3 +73,45 @@
(let ((data (read-2 file))) (let ((data (read-2 file)))
(time (progn (print (%solve-2 data #'%solve-2-1)) (time (progn (print (%solve-2 data #'%solve-2-1))
(print (%solve-2 data #'%solve-2-2)))))) (print (%solve-2 data #'%solve-2-2))))))
(defun read-3 (file)
(with-open-file (stream file)
(let ((temp-map (loop for line = (read-line stream nil)
while line
collect line)))
(make-array (list (length temp-map) (length (first temp-map)))
:initial-contents temp-map
:element-type 'character))))
(defun step-slope (position direction map)
(let* ((temp-position (map 'list #'+ direction position))
(overflow (map 'list #'> temp-position (array-dimensions map))))
(cond ((first overflow) :bottom)
((second overflow)
(progn (decf (second temp-position) (second (array-dimensions map)))
temp-position))
(t temp-position))))
(defun solve-3-1 (direction map)
(let ((tree-count 0))
(labels ((rec (position)
(let ((zero-pos (map 'list #'1- position))
(new-pos (step-slope position direction map)))
(if (char= (aref map (first zero-pos) (second zero-pos))
#\#)
(incf tree-count))
(unless (eq new-pos :bottom)
(rec new-pos)))))
(rec '(1 1))
tree-count)))
;; (height width)
(defun solve-3-2 (slopes map)
(reduce #'* (loop for slope in slopes
collecting (solve-3-1 slope map))))
(defun solve-3 (map)
(time (let ((data (read-3 map)))
(print (solve-3-1 '(1 3) data))
(print (solve-3-2 '((1 1) (1 3) (1 5) (1 7) (2 1)) data)))))

Loading…
Cancel
Save