-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-07.lisp
26 lines (21 loc) · 863 Bytes
/
day-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
(defpackage :day-07
(:use :cl)
(:import-from :utils :read-day-file)
(:import-from :alexandria :alist-hash-table :hash-table-values)
(:export #:part-1 #:part-2))
(in-package :day-07)
(defun initial-list ()
(let ((s (car (read-day-file "07"))))
(read-from-string (concatenate 'string "(" (substitute #\Space #\, s) ")"))))
(defun compute-total-cost (cost-func)
(let* ((lst (initial-list))
(table (alist-hash-table (mapcar #'(lambda (c) (cons c 0)) lst))))
(loop for proposed in lst
do (setf (gethash proposed table)
(reduce #'+ (mapcar #'(lambda (n) (funcall cost-func (abs (- n proposed)))) lst))))
(car (sort (hash-table-values table) #'<))))
(defun part-1 ()
(compute-total-cost #'identity))
(defun part-2 ()
(flet ((func (diff) (* (/ diff 2) (+ 1 diff))))
(compute-total-cost #'func)))