diff options
Diffstat (limited to '2024/10/puzzles.lisp')
| -rw-r--r-- | 2024/10/puzzles.lisp | 42 | 
1 files changed, 42 insertions, 0 deletions
| diff --git a/2024/10/puzzles.lisp b/2024/10/puzzles.lisp new file mode 100644 index 0000000..696bb25 --- /dev/null +++ b/2024/10/puzzles.lisp @@ -0,0 +1,42 @@ +#!/usr/bin/sbcl --script + + +(defun main (filename) +  (let ((lines (read-file-to-lines filename))) +    (loop 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 "~a~%" (main "input"))
\ No newline at end of file |