From a17acf6d9e8119b837fd440b4e17c191ff2564bd Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Tue, 24 Nov 2020 01:23:11 -0500 Subject: [PATCH] Add project structure and Day 00 code (2019/Day 01) --- adventofcode2020.asd | 23 +++++++++++++++++++++++ package.lisp | 7 +++++++ src/adventofcode2020.lisp | 13 +++++++++++++ src/day00.lisp | 30 ++++++++++++++++++++++++++++++ src/utilities.lisp | 9 +++++++++ t/adventofcode2020.lisp | 3 +++ t/day00.lisp | 2 ++ 7 files changed, 87 insertions(+) create mode 100644 adventofcode2020.asd create mode 100644 package.lisp create mode 100644 src/adventofcode2020.lisp create mode 100644 src/day00.lisp create mode 100644 src/utilities.lisp create mode 100644 t/adventofcode2020.lisp create mode 100644 t/day00.lisp diff --git a/adventofcode2020.asd b/adventofcode2020.asd new file mode 100644 index 0000000..96f4fbd --- /dev/null +++ b/adventofcode2020.asd @@ -0,0 +1,23 @@ +;;;; adventofcode2020.asd + +(asdf:defsystem #:adventofcode2020 + :description "Advent of Code 2020" + :author "Jack Kinsey " + :version "0.0.1" + :serial t + :depends-on (:alexandria + :arrow-macros + :fiveam) + :pathname "src/" + :components ((:file "package") + (:file "adventofcode2020")) + :in-order-to ((test-op (test-op :adventofcode2020/test)))) + +(asdf:defsystem #:adventofcode2020/tests + :description "Advent of Code 2020 test suite" + :author "Jack Kinsey " + :depends-on (:adventofcode2020 :fiveam) + :pathname "t/" + :components ((:file "package") + (:file "adventofcode2020")) + :perform (test-op (o c) (symbol-call :5am :run! :adventofcode2020))) diff --git a/package.lisp b/package.lisp new file mode 100644 index 0000000..363adb0 --- /dev/null +++ b/package.lisp @@ -0,0 +1,7 @@ +(defpackage #:adventofcode2020 + (:use #:cl + #:arrow-macros + #:alexandria + #:fiveam) + (:import-from :cl-ppcre :split) + (:nicknames :aoc2020)) diff --git a/src/adventofcode2020.lisp b/src/adventofcode2020.lisp new file mode 100644 index 0000000..54a6aca --- /dev/null +++ b/src/adventofcode2020.lisp @@ -0,0 +1,13 @@ +(in-package #:adventofcode2020) + +(defmacro day (num var &body body) + (let ((day-str (format nil "day~2,'0D" num))) + `(let ((,var (alexandria:read-file-into-string ,(format nil "./res/~A" day-str)))) + ,@body))) + +(defun part1 (str) + (format t "Part 1: ~A~%" str)) + +(defun part2 (str) + (format t "Part 2: ~A~%" str)) + diff --git a/src/day00.lisp b/src/day00.lisp new file mode 100644 index 0000000..a6e755c --- /dev/null +++ b/src/day00.lisp @@ -0,0 +1,30 @@ +(in-package #:adventofcode2020) + +(defun simple-fuel (mass) + (- (floor (/ mass 3)) + 2)) + +(defun complex-fuel (mass) + (loop with m = (simple-fuel mass) + while (> m 0) sum m + do (setf m (simple-fuel m)))) + +(day 00 input + (let ((modules (int-list-from input))) + (part1 (reduce #'+ (mapcar #'simple-fuel modules))) + (part2 (reduce #'+ (mapcar #'complex-fuel modules))))) + +(def-suite day00) +(in-suite day00) + +(test simple-fuel + (is (equal + '(2 2 654 33583) + (mapcar #'simple-fuel '(12 14 1969 100756))))) + +(test complex-fuel + (is (equal + '(2 966 50346) + (mapcar #'complex-fuel '(14 1969 100756))))) + +(run! 'day00) diff --git a/src/utilities.lisp b/src/utilities.lisp new file mode 100644 index 0000000..c677e96 --- /dev/null +++ b/src/utilities.lisp @@ -0,0 +1,9 @@ +(in-package #:adventofcode2020) + +;;; Utility functions + +(defun list-from (str) + (split "\\n" str)) + +(defun int-list-from (str) + (mapcar #'parse-integer (split "\\n" str))) diff --git a/t/adventofcode2020.lisp b/t/adventofcode2020.lisp new file mode 100644 index 0000000..9078932 --- /dev/null +++ b/t/adventofcode2020.lisp @@ -0,0 +1,3 @@ +;;;; adventofcode2020.lisp + +(in-package #:adventofcode2020) diff --git a/t/day00.lisp b/t/day00.lisp new file mode 100644 index 0000000..b090757 --- /dev/null +++ b/t/day00.lisp @@ -0,0 +1,2 @@ +(in-package #:adventofcode2020) + -- 2.38.5