Skip to content

Instantly share code, notes, and snippets.

@death
Created December 15, 2021 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save death/4f0194b687b611df6480f50f981c1a68 to your computer and use it in GitHub Desktop.
Save death/4f0194b687b611df6480f50f981c1a68 to your computer and use it in GitHub Desktop.
aoc2021 day15
;;;; +----------------------------------------------------------------+
;;;; | Advent of Code 2021 |
;;;; +----------------------------------------------------------------+
(defpackage #:snippets/aoc2021/day15
(:use #:cl)
(:import-from
#:snippets/slhelpers)
(:export
#:day15))
(in-package #:snippets/aoc2021/day15)
(defun least-risk (cave scale)
(let* ((n (length (aref cave 0)))
(m (length cave))
(goal-position (complex (1- (* scale n)) (1- (* scale m)))))
(snippets/slhelpers:a-star
#C(0 0)
:goal-state-p
(lambda (pos)
(= pos goal-position))
:heuristic
(constantly 0)
:expand
(lambda (pos)
(loop for dir in '(#C(-1 0) #C(+1 0) #C(0 -1) #C(0 +1))
for neighbor = (+ pos dir)
when (and (<= 0 (realpart neighbor) (realpart goal-position))
(<= 0 (imagpart neighbor) (imagpart goal-position)))
collect (multiple-value-bind (q1 r1) (truncate (realpart neighbor) n)
(multiple-value-bind (q2 r2) (truncate (imagpart neighbor) m)
(list (1+ (mod (+ (1- (digit-char-p (aref (aref cave r2) r1))) q1 q2) 9))
neighbor))))))))
(defun day15 (input)
(let ((cave (coerce input 'vector)))
(list (nth-value 1 (least-risk cave 1))
(nth-value 1 (least-risk cave 5)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment