You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
#+title: Solution to p3 |
|
|
|
First load the data as a list of lists of numbers |
|
#+begin_src emacs-lisp :results none |
|
(with-temp-buffer |
|
(insert-file-contents "input") |
|
(advent/replace-multiple-regex-buffer |
|
'(("\\(.\\)" . "\\1 ") |
|
("^" . "(") |
|
("$" . ")"))) |
|
(goto-char (point-min)) |
|
(insert "(setq data '(") |
|
(goto-char (point-max)) |
|
(insert "))") |
|
(eval-buffer)) |
|
#+end_src |
|
|
|
This is for part 1: |
|
#+begin_src emacs-lisp |
|
(defun joltage (bank) |
|
(let* ((max-joltage (-max bank)) |
|
(max-pos (1+ (-elem-index max-joltage bank)))) |
|
(if (< max-pos (length bank)) |
|
(+ (* 10 max-joltage) (-max (-drop max-pos bank))) |
|
(+ (* 10 (-max (-drop-last 1 bank))) max-joltage)))) |
|
|
|
(-sum (-map #'joltage data)) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
: 17405 |
|
|
|
Let us redo it with a recursive approach |
|
#+begin_src emacs-lisp |
|
(setq max-batteries 12) |
|
|
|
(defun joltage (bank no-batteries) |
|
(if (< no-batteries 0) 0 |
|
(let ((mult (expt 10 no-batteries)) |
|
(max-available-joltage (-max (-drop-last no-batteries bank)))) |
|
(+ (* mult max-available-joltage) |
|
(joltage (-drop (1+ (-elem-index max-available-joltage bank)) bank) |
|
(- no-batteries 1)))))) |
|
|
|
(-sum (--map (joltage it (- max-batteries 1)) data)) |
|
|
|
#+end_src |
|
|
|
#+RESULTS: |
|
: 171990312704598
|
|
|