second task of day 1, cool

This commit is contained in:
efim 2022-12-01 15:14:12 +00:00
parent 0116b13088
commit 455cee8c79
1 changed files with 60 additions and 0 deletions

View File

@ -63,3 +63,63 @@ my-int
;; (in "day1-input.txt") ;; (in "day1-input.txt")
(calc-max-inventory-calories "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