-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lisp
48 lines (40 loc) · 1.48 KB
/
util.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
(in-package #:advent-of-code-2023.util)
(defun transpose (l)
(apply #'mapcar #'list l))
(defun histogram (list)
"Calculate frequency histogram of LIST in the form of a hash table."
(loop
with h = (make-hash-table)
for el in list
do (incf (gethash el h 0))
finally (return h)))
(defun split-lines (string)
"Helper function to split STRING into a list of lines."
(uiop:split-string string :separator '(#\Newline)))
(defun square (x)
(* x x))
(setf (fdefinition '^2) #'square)
(defun join-numbers (a b)
"Combine numbers A and B by juxtaposition."
(+ (* a (expt 10 (ceiling (log b 10)))) b))
(defun position-in-2d-array (item array)
"Look for ITEM in a 2-dimensional ARRAY. When found, return its
coordinates in the form (row, column). Otherwise, return NIL."
(destructuring-bind (d1 d2) (array-dimensions array)
(loop
for r from 0 below d1
do (loop
for c from 0 below d2
when (equal item (aref array r c))
do (return-from position-in-2d-array (list r c))))))
(defun parse-string-into-array (data &key adjustable)
"Parse DATA string into a 2-dimensional array."
(let ((char-array (mapcar (alexandria:rcurry #'coerce 'list)
(split-lines data))))
(make-array (list (length char-array) (length (first char-array)))
:adjustable adjustable
:initial-contents char-array)))
(defun parse-string-into-list (data)
"Parse DATA string into a 2-dimensional list."
(mapcar (alexandria:rcurry #'coerce 'list)
(split-lines data)))