From 455cee8c79e8818da68cfe2c503bb6e1b0236157 Mon Sep 17 00:00:00 2001 From: efim Date: Thu, 1 Dec 2022 15:14:12 +0000 Subject: [PATCH] second task of day 1, cool --- day1.lisp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/day1.lisp b/day1.lisp index 5024d9a..ad70342 100644 --- a/day1.lisp +++ b/day1.lisp @@ -63,3 +63,63 @@ my-int ;; (in "day1-input.txt") (calc-max-inventory-calories "day1-input.txt") + +;;; and second task - find TOP 3 inventory sums, and what is their total +;; how'd i do that? +;; would be cool to do a class, that would be able to store the top 3 +;; and would have single method to check and possibly include a new number with eviction of the lovest number +;; do I go for CLOS here? +;; ugh. + +(setq my-prev-max (list 1 4 5 7)) +(apply #'min my-prev-max) +(set-difference my-prev-max (list 1)) +my-prev-max +(set-difference my-prev-max (list (apply #'min my-prev-max))) + +;; so, i could use that on the "found max" +;; could also store "cur minmal maximul" + +(defun calc-3-max-inventory-calories (filename) + (setq current-sum 0) + (setq current-3-max '()) + (with-open-file + (in filename) + (loop + for line = (read-line in nil nil) + while line + if (string-equal line "") + do (progn + ;; current inventory ended, + ;; check whether it's new max and null accum + (setq current-3-max (cons current-sum current-3-max)) + (if (< 3 (length current-3-max)) + (setq current-3-max + (set-difference current-3-max (list (apply #'min current-3-max)))) + ) + (setq current-sum 0)) + else do (setq current-sum (+ current-sum (parse-integer line))) + )) + (apply #'+ current-3-max)) + +(append (list 12 1 14) 15) +(setq my-prepend (cons 15 (list 12 1 14))) +(apply #'+ (list 1 2 3)) + +(calc-3-max-inventory-calories "day1-input.txt") + +current-3-max +(> 3 2) + +;; ok. so notes for today: +;; 1. i'm messing up (> 3 1) in conditions +;; 2. I really should start using #'LET instead of #'SETQ +;; SETQ are for defining what? global names? these are set and available outside of the function +;; even though i didn't create them as DEFVAR? Or maybe I did, for the first function +;; ok then +;; 3. looking in Common Lisp Cookbook : https://lispcookbook.github.io/cl-cookbook/iteration.html +;; for the examples of the loops and set operations +;; and another tutorial with sample of reading the file https://riptutorial.com/common-lisp/example/10284/reading-file +;; and https://www.tutorialspoint.com/lisp/lisp_file_io.htm +;; and https://stackoverflow.com/questions/44466001/read-from-file-common-lisp +;; 4. Sad that I don't really look into the efficient ways of doing things