diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-02-05 21:59:47 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-02-05 22:49:20 +0100 |
commit | 57e563bf8f4fae7364118619318b338f16792473 (patch) | |
tree | c21701f70f1440fa8078531929e883f4437e9577 | |
parent | 4daf41bd69e01f137be90881ec419b3c045a2ec5 (diff) | |
download | scratch-57e563bf8f4fae7364118619318b338f16792473.tar.gz scratch-57e563bf8f4fae7364118619318b338f16792473.tar.bz2 scratch-57e563bf8f4fae7364118619318b338f16792473.zip |
working with more stuff
-rwxr-xr-x | AoC2022/01/solver.lisp | 40 | ||||
-rw-r--r-- | AoC2022/aocclj/src/aocclj/day01.clj | 7 | ||||
-rw-r--r-- | AoC2022/readme.org | 2 |
3 files changed, 37 insertions, 12 deletions
diff --git a/AoC2022/01/solver.lisp b/AoC2022/01/solver.lisp index d39bb7c..dd09c07 100755 --- a/AoC2022/01/solver.lisp +++ b/AoC2022/01/solver.lisp @@ -2,22 +2,46 @@ (ql:quickload '(:uiop :fiveam)) -(defun elves-rations () +(defun elves-rations (input) (reduce (lambda (acc value) - (if (string= value "") + (if (equal value "") (cons 0 acc) (progn (incf (car acc) (parse-integer value)) acc))) - (uiop:read-file-lines "input") + input :initial-value (list 0))) +(defun elves-rations-nc (filename) + (with-open-file (in filename) + (let ((acc (list 0))) + (loop for line = (read-line in nil) while line + for num = (parse-integer line :junk-allowed t) + if (numberp num) do + (incf (car acc) num) + else do + (push 0 acc)) + acc))) + +(defun elves-rations-max (filename) + (with-open-file (in filename) + (let ((max 0) + (elf 0)) + (loop for line = (read-line in nil) while line + for num = (parse-integer line :junk-allowed t) + if (numberp num) do + (incf elf num) + else do + (setf max (max elf max)) + (setf elf 0)) + (max elf max)))) + (fiveam:test results - (let ((rations (elves-rations))) + (let ((rations (elves-rations (uiop:read-file-lines "input")))) ;; calculate the maximum from each elves rations - (fiveam:is (= 75622 (apply #'max rations))) + (fiveam:is (= 75622 (reduce #'max rations))) + (fiveam:is (= 75622 (reduce #'max (elves-rations-nc "input")))) + (fiveam:is (= 75622 (elves-rations-max "input"))) ;; calculate the maximum from each elves rations - (fiveam:is (= 213159 (apply #'+ (subseq (sort rations #'>) 0 3)))))) - - + (fiveam:is (= 213159 (reduce #'+ (subseq (sort rations #'>) 0 3)))))) diff --git a/AoC2022/aocclj/src/aocclj/day01.clj b/AoC2022/aocclj/src/aocclj/day01.clj index 8fca2e6..df6d101 100644 --- a/AoC2022/aocclj/src/aocclj/day01.clj +++ b/AoC2022/aocclj/src/aocclj/day01.clj @@ -2,17 +2,16 @@ (:require [clojure.string :as str])) (defn preprocess [input] - (transduce + (sequence (comp (map parse-long) (partition-by nil?) (take-nth 2) (map (partial reduce +))) - conj input)) (defn part1 [input] - (apply max (preprocess input))) + (reduce max (preprocess input))) (defn part2 [input] - (->> input preprocess sort (take 3) (apply +))) + (->> input preprocess (sort >) (take 3) (reduce +))) diff --git a/AoC2022/readme.org b/AoC2022/readme.org index ae155cb..cab252c 100644 --- a/AoC2022/readme.org +++ b/AoC2022/readme.org @@ -6,3 +6,5 @@ From any directory to execute the solutions call #+begin_src bash make -C .. <language> #+end_src +* Review on other languages +https://github.com/abyala/advent-2022-clojure |