blob: d67b075641f67202720dfc1342eade53477b1ba0 (
plain) (
blame)
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
|
#!/usr/bin/sbcl --script
(defun main (filename)
(loop with lines = (read-file-to-lines filename)
with dimensions = (array-dimensions lines)
for i from 0 below (first dimensions)
summing (loop for j from 0 below (second dimensions)
when (char= #\0 (aref lines i j))
summing (score-for-trail-head lines i j))))
(defun read-file-to-lines (filename)
(with-open-file (stream filename)
(let ((lines (loop for line = (read-line stream nil)
while line
collect (coerce line 'array))))
(make-array (list (length lines)
(length (first lines)))
:initial-contents lines))))
(defun score-for-trail-head (lines i j)
(let* ((positions (positions-of-nines lines i j))
;; START PART 1
(positions (remove-duplicates positions :test 'equal))
;; END PART 1
)
(length positions)))
(defun positions-of-nines (lines i j)
(let ((char (aref lines i j)))
(if (char= #\9 char)
(list (cons i j))
(loop with needs = (code-char (1+ (char-code char)))
with dimensions = (array-dimensions lines)
for (i . j) in (list (cons (1- i) j) (cons i (1- j))
(cons (1+ i) j) (cons i (1+ j)))
when (and (< -1 i (first dimensions))
(< -1 j (second dimensions))
(char= (aref lines i j) needs))
append (positions-of-nines lines i j)))))
(format t "~d~%" (main "input"))
|