Day 05 - Reuse state of part 1 as a base for part 2
This commit is contained in:
parent
0cacd9883d
commit
69b0ec0568
|
@ -52,10 +52,8 @@
|
||||||
until (and (= x (point-x point2))
|
until (and (= x (point-x point2))
|
||||||
(= y (point-y point2)))))))
|
(= y (point-y point2)))))))
|
||||||
|
|
||||||
(defun build-count-hash (lines)
|
(defun mark-lines (lines hash)
|
||||||
(let ((hash (make-hash-table :test 'equal)))
|
(dolist (line lines) (expand-line line hash)))
|
||||||
(dolist (line lines) (expand-line line hash))
|
|
||||||
hash))
|
|
||||||
|
|
||||||
(defun angled-line-p (line)
|
(defun angled-line-p (line)
|
||||||
(let ((p1 (line-start line))
|
(let ((p1 (line-start line))
|
||||||
|
@ -63,19 +61,22 @@
|
||||||
(and (/= (point-x p1) (point-x p2))
|
(and (/= (point-x p1) (point-x p2))
|
||||||
(/= (point-y p1) (point-y p2)))))
|
(/= (point-y p1) (point-y p2)))))
|
||||||
|
|
||||||
(defun count-crossing-points (lines)
|
(defun reverse-partition (pred col)
|
||||||
(let ((count-hash (build-count-hash lines)))
|
(loop with yays = '()
|
||||||
(loop for k being the hash-keys in count-hash using (hash-value v)
|
with nays = '()
|
||||||
count (>= v 2))))
|
for e in col
|
||||||
|
do (if (funcall pred e)
|
||||||
|
(setf yays (cons e yays))
|
||||||
|
(setf nays (cons e nays)))
|
||||||
|
finally (return (list yays nays))))
|
||||||
|
|
||||||
(defun part1 (input)
|
(defun count-crossing-points (lines count-hash)
|
||||||
(count-crossing-points (remove-if #'angled-line-p input)))
|
(mark-lines lines count-hash)
|
||||||
|
(loop for k being the hash-keys in count-hash using (hash-value v)
|
||||||
|
count (>= v 2)))
|
||||||
|
|
||||||
(defun part2 (input)
|
(time
|
||||||
(count-crossing-points input))
|
(let ((hash (make-hash-table :test 'equal))
|
||||||
|
(lines (reverse-partition #'angled-line-p *data*)))
|
||||||
(format t "===== Part 1 =====")
|
(format t "Part 1: ~A~%" (count-crossing-points (cadr lines) hash))
|
||||||
(format t "Result: ~A~%~%" (time (part1 *data*)))
|
(format t "Part 2: ~A~%" (count-crossing-points (car lines) hash))))
|
||||||
|
|
||||||
(format t "===== Part 2 =====")
|
|
||||||
(format t "Result: ~A~%" (time (part2 *data*)))
|
|
||||||
|
|
Reference in New Issue