aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day09/solver.lisp
blob: 647284f726e3a27bcfc1f1f33a39fdcd2e46fbdb (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
37
38
;;03:39
;;04:07 Solved
;;05:24 Painful refactorins
(ql:quickload '(fiveam arrows transducers))

(defparameter eg-input "0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45")

(defun row-reducer (row)
  (mapcar #'- (cdr row) row))

(defun terminal-p (row)
  (every #'= (cdr row) row))

(defun get-coefs (row &optional (acc 0))
  (if (terminal-p row)
      (+ (car (last row)) acc)
      (get-coefs (row-reducer row) (+ (car (last row)) acc))))

(defun parse-line (line)
  (mapcar #'parse-integer (uiop:split-string line)))

(defun solver (lines order)
  (reduce #'+
          lines
          :key (alexandria:compose #'get-coefs order #'parse-line)))

(fiveam:test solutions
  (fiveam:is (= 114 (solver (uiop:split-string eg-input :separator '(#\Newline))
                            #'identity)))
  (fiveam:is (= 2105961943 (solver (uiop:read-file-lines "input")
                                   #'identity)))

  (fiveam:is (= 2 (solver (uiop:split-string eg-input :separator '(#\Newline))
                          #'nreverse)))
  (fiveam:is (= 1019 (solver (uiop:read-file-lines "input")
                             #'nreverse))))