aboutsummaryrefslogtreecommitdiff
path: root/2024/01/puzzle-1.el
blob: 622e9e528f0ec8399498b7268c1e9990142f9f88 (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)))))
    (cl-loop for i from 0 below (length nums)
             for x = (aref nums i)
             if (cl-evenp i)
               collect x into xs
             else
               collect x into ys
             finally return (thread-last
                              (seq-mapn #'- (sort xs) (sort ys))
                              (mapcar #'abs)
                              (apply #'+)))))

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