From 57e563bf8f4fae7364118619318b338f16792473 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sun, 5 Feb 2023 21:59:47 +0100 Subject: working with more stuff --- AoC2022/01/solver.lisp | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'AoC2022/01/solver.lisp') diff --git a/AoC2022/01/solver.lisp b/AoC2022/01/solver.lisp index d39bb7c..dd09c07 100755 --- a/AoC2022/01/solver.lisp +++ b/AoC2022/01/solver.lisp @@ -2,22 +2,46 @@ (ql:quickload '(:uiop :fiveam)) -(defun elves-rations () +(defun elves-rations (input) (reduce (lambda (acc value) - (if (string= value "") + (if (equal value "") (cons 0 acc) (progn (incf (car acc) (parse-integer value)) acc))) - (uiop:read-file-lines "input") + input :initial-value (list 0))) +(defun elves-rations-nc (filename) + (with-open-file (in filename) + (let ((acc (list 0))) + (loop for line = (read-line in nil) while line + for num = (parse-integer line :junk-allowed t) + if (numberp num) do + (incf (car acc) num) + else do + (push 0 acc)) + acc))) + +(defun elves-rations-max (filename) + (with-open-file (in filename) + (let ((max 0) + (elf 0)) + (loop for line = (read-line in nil) while line + for num = (parse-integer line :junk-allowed t) + if (numberp num) do + (incf elf num) + else do + (setf max (max elf max)) + (setf elf 0)) + (max elf max)))) + (fiveam:test results - (let ((rations (elves-rations))) + (let ((rations (elves-rations (uiop:read-file-lines "input")))) ;; calculate the maximum from each elves rations - (fiveam:is (= 75622 (apply #'max rations))) + (fiveam:is (= 75622 (reduce #'max rations))) + (fiveam:is (= 75622 (reduce #'max (elves-rations-nc "input")))) + (fiveam:is (= 75622 (elves-rations-max "input"))) ;; calculate the maximum from each elves rations - (fiveam:is (= 213159 (apply #'+ (subseq (sort rations #'>) 0 3)))))) - - + (fiveam:is (= 213159 (reduce #'+ (subseq (sort rations #'>) 0 3)))))) -- cgit v1.2.3