Day 05 - Reuse state of part 1 as a base for part 2

This commit is contained in:
aru 2021-12-07 19:30:30 +01:00
parent 0cacd9883d
commit 69b0ec0568
1 changed files with 19 additions and 18 deletions

View File

@ -52,10 +52,8 @@
until (and (= x (point-x point2))
(= y (point-y point2)))))))
(defun build-count-hash (lines)
(let ((hash (make-hash-table :test 'equal)))
(dolist (line lines) (expand-line line hash))
hash))
(defun mark-lines (lines hash)
(dolist (line lines) (expand-line line hash)))
(defun angled-line-p (line)
(let ((p1 (line-start line))
@ -63,19 +61,22 @@
(and (/= (point-x p1) (point-x p2))
(/= (point-y p1) (point-y p2)))))
(defun count-crossing-points (lines)
(let ((count-hash (build-count-hash lines)))
(loop for k being the hash-keys in count-hash using (hash-value v)
count (>= v 2))))
(defun reverse-partition (pred col)
(loop with yays = '()
with nays = '()
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)
(count-crossing-points (remove-if #'angled-line-p input)))
(defun count-crossing-points (lines count-hash)
(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)
(count-crossing-points input))
(format t "===== Part 1 =====")
(format t "Result: ~A~%~%" (time (part1 *data*)))
(format t "===== Part 2 =====")
(format t "Result: ~A~%" (time (part2 *data*)))
(time
(let ((hash (make-hash-table :test 'equal))
(lines (reverse-partition #'angled-line-p *data*)))
(format t "Part 1: ~A~%" (count-crossing-points (cadr lines) hash))
(format t "Part 2: ~A~%" (count-crossing-points (car lines) hash))))