(list 1 2 3) ;;; input - one number per line, different intentories separated by empty new line ;; need to find what - how much does have the intventory with maximal sum ;; so, do i have a function that takes in one string? ;; i guess it would be better to read input from the separate file ;; would also probably be better to read file line a time (defvar current-sum 0) (defvar current-max -1) (with-open-file (in "day1-input.txt") (loop for line = (read-line in nil nil) while line ;; (if (eq line = "") (print "got empty line")) ;; collect line ;; do (format t "got line ~A~%" line) ;;; yay, this is somewhat what i want ;; if (string-equal line "") do (format t "got empty line ~A~% in if" line) ;; else do (format t "else branch for nonempty line ~A~%" line) if (string-equal line "") do (progn ;; current inventory ended, ;; check whether it's new max and null accum (if (< current-max current-sum) (setq current-max current-sum) ) (setq current-sum 0)) else do (setq current-sum (+ current-sum (parse-integer line))) )) current-max ; i got a right answer ;; yay, now i can write a stateful function that gets one line at a time ;; ;; what i could probably do is have ;; if clauses directly in the loop ;; and additional variables defined and updated in the loop as well (setq my-int (parse-integer "10497")) my-int ;; let's wrap this into single function, i suppose (defun calc-max-inventory-calories (filename) (setq current-sum 0) (setq current-max -1) (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 (if (< current-max current-sum) (setq current-max current-sum) ) (setq current-sum 0)) else do (setq current-sum (+ current-sum (parse-integer line))) )) current-max) ;; (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