Created
December 1, 2020 11:00
-
-
Save death/58bb55df8848430e590e40d3619cc2fd to your computer and use it in GitHub Desktop.
aoc2020 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 2020 | | |
;;;; +----------------------------------------------------------------+ | |
(defpackage #:snippets/aoc2020/day1 | |
(:use #:cl) | |
(:shadowing-import-from | |
#:screamer | |
#:defun | |
#:a-member-of | |
#:either | |
#:fail | |
#:assert! | |
#:one-value) | |
(:export | |
#:day1)) | |
(in-package #:snippets/aoc2020/day1) | |
(defun product (sequence) | |
(reduce #'* sequence :initial-value 1)) | |
(defun sum (sequence) | |
(reduce #'+ sequence :initial-value 0)) | |
(defun a-k-subset-of (list k) | |
(cond ((zerop k) '()) | |
((null list) (fail)) | |
(t (either | |
(cons (first list) (a-k-subset-of (rest list) (1- k))) | |
(a-k-subset-of (rest list) k))))) | |
(defun k-sum-to (numbers k goal) | |
(let ((summands (a-k-subset-of numbers k))) | |
(assert! (= (sum summands) goal)) | |
summands)) | |
(defun day1 (entries) | |
(list (product (one-value (k-sum-to entries 2 2020))) | |
(product (one-value (k-sum-to entries 3 2020))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment