-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.lisp
50 lines (28 loc) · 1.3 KB
/
07.lisp
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
39
40
41
42
43
44
45
46
47
48
49
50
;;; Advent of Code Day 7
;;; J Jensen
;;; Requires split-sequence
(defconstant +day7-test-input+ "16,1,2,0,4,2,7,1,2,14")
(defun read-crabs-from-string (str)
(mapcar #'parse-integer (split-sequence:split-sequence #\, str)))
(defun fuel-for-move (crabs pos)
"Calculate the fuel required for a move to position pos"
(apply #'+ (mapcar (lambda (j) (abs (- pos j))) crabs)))
(defun search-min-pos (crabs fuel-func)
"Find a minimum fuel for crabs"
(let* ((cmin (apply #'min crabs))
(cmax (apply #'max crabs)))
;; simple method, though an actual search may be feasible
(loop for pos from cmin upto cmax minimizing (funcall fuel-func crabs pos))))
(defun solve-input (input fuel-func)
(etypecase input
(string (search-min-pos (read-crabs-from-string input) fuel-func))
(stream (solve-input (read-line input nil) fuel-func))
(pathname (with-open-file (f input :direction :input :if-does-not-exist :error) (solve-input f fuel-func)))))
(defun solve1 (input)
(solve-input input #'fuel-for-move))
(defun fuel-for-move-2 (crabs pos)
"Calculate the fuel required for a move to position pos"
(flet ((triangle-number (k) (/ (* k (1+ k)) 2)))
(apply #'+ (mapcar (lambda (j) (triangle-number (abs (- pos j)))) crabs))))
(defun solve2 (input)
(solve-input input #'fuel-for-move-2))