aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/01/solver.el
blob: 8dba125d0dbbdf3aaffbed9b216d2a464cf57f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pre { line-height: 125%; }
td.linenos .normal { color: #37474F; background-color: #263238; padding-left: 5px; padding-right:
;;; solver.el --- Solution for AoC 2022 day 1 -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: December 02, 2022
;; Modified: December 02, 2022
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Solution for AoC 2022 day 1
;;
;;; Code:

(require 'seq)
(require 'subr-x)

(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))))))
rest 2) (write-snafu (1+ div) (cons (encode-char (- rest 5)) coeffs)) (write-snafu div (cons (encode-char rest) coeffs)))))) (defun solver (filename) (write-snafu (apply #'+ (mapcar #'parse-snafu (uiop:read-file-lines filename))))) (fiveam:test solutions (fiveam:is (string= "2=-1=0" (solver "eg-in"))) (fiveam:is (string= "2-212-2---=00-1--102" (solver "input"))))