From d65a7abed2bbf6ad8f5630963e583df62cca63eb Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sat, 16 Dec 2023 13:26:50 +0100 Subject: solution part2 --- AoC2023/day14/solve.lisp | 76 ------------------------------------------------ 1 file changed, 76 deletions(-) delete mode 100644 AoC2023/day14/solve.lisp (limited to 'AoC2023/day14/solve.lisp') diff --git a/AoC2023/day14/solve.lisp b/AoC2023/day14/solve.lisp deleted file mode 100644 index e476bd5..0000000 --- a/AoC2023/day14/solve.lisp +++ /dev/null @@ -1,76 +0,0 @@ -;; 9:39 start -;; 10:17 part 1 -;; -(ql:quickload '(fiveam)) - -(defun roll (direction row col) - (ecase direction - (north (list (1- row) col)) - (south (list (1+ row) col)) - (west (list row (1- col))) - (east (list row (1+ col))))) - -(defun in-bounds (field row col) - (destructuring-bind (maxrow maxcol) (array-dimensions field) - (and (< -1 row maxrow) - (< -1 col maxcol)))) - -(defun roll-rocks (field direction) - (loop for i below (array-dimension field 0) do - (loop for j below (array-dimension field 1) do - (loop - for (row col) = (list i j) then (list new-row new-col) - for (new-row new-col) = (roll direction row col) - while (and - (in-bounds field new-row new-col) - (eq #\O (aref field row col)) - (eq #\. (aref field new-row new-col))) - do (setf (aref field new-row new-col) #\O - (aref field row col) #\.))))) - -(defun array-slice (arr row) - (make-array (array-dimension arr 1) - :displaced-to arr - :displaced-index-offset (* row (array-dimension arr 1)))) - -(defun north-load (field) - (loop - with rows = (array-dimension field 0) - for row below rows - sum (* (- rows row) (count #\O (array-slice field row))))) - -(defun solve1 (input) - (let* ((rows (length input)) - (field - (make-array (list rows (length (car input))) :initial-contents input))) - (roll-rocks field 'north) - (north-load field))) - -(defun draw-field (field &optional (out *standard-output*)) - (destructuring-bind (maxrow maxcol) (array-dimensions field) - (loop for i below maxrow do - (loop for j below maxcol do - (write-char (aref field i j) out)) - (terpri out)) - (terpri out))) - -(defun solve2 (input iters) - (let* ((rows (length input)) - (field - (make-array (list rows (length (car input))) :initial-contents input))) - (loop - do (dolist (dir '(north west south east)) - (roll-rocks-full field dir)) - repeat iters - collect - (north-load field)))) - -;; (solve2 (uiop:read-file-lines "eg-in") 1000000000) -;; (solve2 (uiop:read-file-lines "eg-in") 100) -;; (solve2 (uiop:read-file-lines "input") 100) -(fiveam:test solutions - (fiveam:is (= 136 (solve1 (uiop:read-file-lines "eg-in")))) - (fiveam:is (= 108935 (solve1 (uiop:read-file-lines "input")))) - ) - -(fiveam:run!) -- cgit v1.2.3