Skip to content

Instantly share code, notes, and snippets.

@death
Created December 1, 2020 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save death/58bb55df8848430e590e40d3619cc2fd to your computer and use it in GitHub Desktop.
Save death/58bb55df8848430e590e40d3619cc2fd to your computer and use it in GitHub Desktop.
aoc2020 day1
;;;; +----------------------------------------------------------------+
;;;; | 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