day 1 first simple task
trying out the loop and reading of the files
This commit is contained in:
parent
48c351c983
commit
0116b13088
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
|
||||
(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")
|
Loading…
Reference in New Issue