Created
December 1, 2021 05:29
-
-
Save death/b11c5c8f03bee2e6f3bd5132ed7418cb to your computer and use it in GitHub Desktop.
aoc2021-day1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;;; +----------------------------------------------------------------+ | |
| ;;;; | Advent of Code 2021 | | |
| ;;;; +----------------------------------------------------------------+ | |
| (defpackage #:snippets/aoc2021/day1 | |
| (:use #:cl) | |
| (:export | |
| #:day1)) | |
| (in-package #:snippets/aoc2021/day1) | |
| (defun count-increasing-sums (input k) | |
| (do ((c 0) | |
| (n (- (length input) k)) | |
| (s1 (reduce #'+ input :end k)) | |
| (i 0 (1+ i))) | |
| ((= i n) c) | |
| (let ((s2 (+ (- s1 (aref input i)) (aref input (+ i k))))) | |
| (when (> s2 s1) | |
| (incf c)) | |
| (setf s1 s2)))) | |
| (defun day1 (input) | |
| (let ((vector (coerce input 'vector))) | |
| (list (count-increasing-sums vector 1) | |
| (count-increasing-sums vector 3)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment