aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-02 13:53:13 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-02 13:53:13 +0100
commitc961041fb211c4e72f306a113ef2bb4b8385be1d (patch)
treee6a6286e05f44d12fe212ec70b58e9b1cf217e93
parent400b581731c25975660b86c57f89463b63c59d38 (diff)
downloadscratch-c961041fb211c4e72f306a113ef2bb4b8385be1d.tar.gz
scratch-c961041fb211c4e72f306a113ef2bb4b8385be1d.tar.bz2
scratch-c961041fb211c4e72f306a113ef2bb4b8385be1d.zip
[AoC2022] 01 refactor
-rw-r--r--AoC2022/01/solver.el40
1 files 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