aboutsummaryrefslogtreecommitdiff
path: root/2024/01/puzzle-1.el
blob: cd8853e862a6ba0cd51f8cc8429f66e6ff198236 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(defun solve (input-file)
  (let* ((nums (with-temp-buffer
                 (insert-file-contents-literally input-file)
                 (goto-char (point-min))
                 (save-excursion
                   (insert ?\[)
                   (goto-char (point-max))
                   (insert ?\]))
                 (read (current-buffer))))
         (num-count (length nums)))
    (cl-loop for i from 0 below num-count
             if (cl-evenp i)
               collect (aref nums i) into xs
             else
               collect (aref nums i) into ys
             finally return (thread-last
                              (seq-mapn #'- (sort xs) (sort ys))
                              (mapcar #'abs)
                              (apply #'+)))))

(message "%d" (solve "input"))