From 070f6560bc16d6d27d304b0f37471cdf6c8c5ff8 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 14 Dec 2021 00:49:33 +0000 Subject: Add final solution to 03 --- src/01.lisp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/01.lisp') diff --git a/src/01.lisp b/src/01.lisp index 9646e40..7f36bf1 100644 --- a/src/01.lisp +++ b/src/01.lisp @@ -1,24 +1,21 @@ -(load "~/quicklisp/setup.lisp") -(ql:quickload "uiop") +(load "../src/common.lisp") -(defun get-file-lines (name) - (uiop:read-file-lines name)) +(defun 01/parse-input-direct (input) + (mapcar 'parse-integer input)) ;; Turn the input file into whatever form you will use for both parts ;; (get-file-lines) and (get-file-string) will be useful -(defun parse-input (input-file) - (mapcar 'parse-integer (get-file-lines input-file))) +(defun 01/parse-input (input-file) + (01/parse-input-direct (get-file-lines input-file))) ;; Loops through two item windows on input and counts each time the first is less than the second -(defun part-a (parsed-input) - (let ((sum 0) (prev 0)) - (dolist (i parsed-input) - (if (> i prev) (progn (incf sum 1) (setf prev i)))) +(defun 01/part-a (parsed-input) + (let ((sum 0) (prev (car parsed-input))) + (dolist (i (cons prev parsed-input)) + (when (> i prev) (incf sum)) + (setf prev i)) sum)) ;; Similar to part a, but with four item windows, comparing the sum of the first 3 with the next 3 -(defun part-b (parsed-input) +(defun 01/part-b (parsed-input) (loop for (w x y z) on parsed-input until (null z) if (< (+ w x y) (+ x y z)) count w)) - -(time (format t "part 1: ~a~%" (part-a (parse-input "../inputs/01.txt")))) -(time (format t "part 1: ~a~%" (part-b (parse-input "../inputs/01.txt")))) -- cgit