aoc-2022/day-1/elisp/solution.el
2022-12-05 01:25:24 -05:00

67 lines
2.2 KiB
EmacsLisp
Executable file

#!/usr/bin/env -S emacs --script
;; -*- lexical-binding: t; -*-
;; Copyright (C) 2022 notroot
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(add-to-list 'load-path "../../")
(require 'ert)
(require 'cl-lib)
(require 'aoc)
(defvar input-file "../input.txt")
(defvar test-input-file "../test-input.txt")
(defun eat (data)
(let ((lines (string-lines data)))
(cl-loop for line in lines
if (not (zerop (length line)))
collect (string-to-number line) into group
else
collect group into groups
and do (setq group nil)
finally (return (if group
(append groups (list group))
groups)))))
(defun sum-calories-per-group (groups)
(let ((sums (seq-map (lambda (g)
(apply '+ g))
groups)))
(seq-sort '>= sums)))
(princ (eat (aoc-read-file test-input-file)) 'external-debugging-output)
(ert-deftest part-1-test ()
(let ((calories (sum-calories-per-group (eat (aoc-read-file test-input-file)))))
(should (equal 24000 (nth 0 calories)))))
(ert-deftest part-1 ()
(let ((calories (sum-calories-per-group (eat (aoc-read-file input-file)))))
(should (equal 68292 (nth 0 calories)))))
(ert-deftest part-2-test ()
(let ((calories (sum-calories-per-group (eat (aoc-read-file test-input-file)))))
(should (equal 45000 (apply '+ (seq-take calories 3))))))
(ert-deftest part-2 ()
(let ((calories (sum-calories-per-group (eat (aoc-read-file input-file)))))
(should (equal 203203 (apply '+ (seq-take calories 3))))))
(ert-run-tests-batch-and-exit)