From 69b0ec05686b07ae9bc37b05b7e1110358574fa3 Mon Sep 17 00:00:00 2001 From: aru Date: Tue, 7 Dec 2021 19:30:30 +0100 Subject: [PATCH] Day 05 - Reuse state of part 1 as a base for part 2 --- 05/solution.lisp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/05/solution.lisp b/05/solution.lisp index f2f6b42..a454547 100644 --- a/05/solution.lisp +++ b/05/solution.lisp @@ -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))))