From c961041fb211c4e72f306a113ef2bb4b8385be1d Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 2 Dec 2022 13:53:13 +0100 Subject: [AoC2022] 01 refactor --- AoC2022/01/solver.el | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/AoC2022/01/solver.el b/AoC2022/01/solver.el index 67c1980..ec5710f 100644 --- a/AoC2022/01/solver.el +++ b/AoC2022/01/solver.el @@ -19,30 +19,24 @@ ;; ;;; Code: (require 'seq) +(defun solver-elves-rations () + (with-temp-buffer + (insert-file-contents "input") + (thread-last + (split-string (buffer-string) "\n\n") + (mapcar + (lambda (elf) + (seq-reduce (lambda (acc x) + (+ acc (string-to-number x))) + (split-string elf) 0)))))) + +(ert-deftest solutions () + (let ((rations (solver-elves-rations))) + ;; calculate the maximum from each elves rations + (should (= 75622 (apply #'max rations))) + ;; calculate the maximum from each elves rations + (should (= 213159 (apply #'+ (seq-take (sort rations #'>) 3)))))) -;; calculate the maximum from each elves rations -(with-temp-buffer - (insert-file-contents "input") - (apply #'max - (mapcar - (lambda (elf) - (seq-reduce (lambda (acc x) - (+ acc (string-to-number x))) - (split-string elf) 0)) - (split-string (buffer-string) "\n\n")))) -;; calculate the maximum from each elves rations -(with-temp-buffer - (insert-file-contents "input") - (apply #'+ - (thread-first - (mapcar - (lambda (elf) - (seq-reduce (lambda (acc x) - (+ acc (string-to-number x))) - (split-string elf) 0)) - (split-string (buffer-string) "\n\n")) - (sort #'>) - (seq-take 3)))) (provide 'solver) ;;; solver.el ends here -- cgit v1.2.3