From 63a814f25cb8eff79f8f566f75b7b5fdac549f5f Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Tue, 1 Dec 2020 17:36:25 -0500 Subject: [PATCH] Add day 01 --- package.lisp | 1 + res/day01 | 200 ++++++++++++++++++++++++++++++++++++++ src/adventofcode2020.lisp | 4 +- src/day01.lisp | 29 ++++++ src/utilities.lisp | 31 ++++++ 5 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 res/day01 create mode 100644 src/day01.lisp diff --git a/package.lisp b/package.lisp index 363adb0..6a6f94b 100644 --- a/package.lisp +++ b/package.lisp @@ -1,3 +1,4 @@ +; (ql:quickload :fiveam) (ql:quickload :cl-ppcre) (defpackage #:adventofcode2020 (:use #:cl #:arrow-macros diff --git a/res/day01 b/res/day01 new file mode 100644 index 0000000..f260c56 --- /dev/null +++ b/res/day01 @@ -0,0 +1,200 @@ +1632 +1438 +1811 +1943 +1883 +1698 +1976 +1972 +1794 +1726 +1850 +1789 +1524 +1701 +1454 +1594 +1655 +1018 +1828 +1867 +1959 +1541 +1596 +1998 +1916 +1894 +1727 +1812 +1800 +1897 +1534 +1712 +1825 +1629 +1827 +81 +1855 +1621 +1694 +1663 +1793 +1685 +1616 +1899 +1688 +1652 +1719 +1589 +1649 +1742 +1905 +922 +1695 +1747 +1989 +1968 +1678 +1709 +1938 +1920 +1429 +1556 +2005 +1728 +1484 +1746 +1702 +1456 +1917 +1670 +1433 +1538 +1806 +1667 +1505 +963 +1478 +2003 +1955 +1689 +1490 +1523 +1615 +1784 +1624 +583 +1465 +1443 +1489 +1873 +1485 +1773 +1704 +352 +505 +1705 +1844 +1599 +1778 +1846 +1533 +1535 +1965 +1987 +828 +1755 +1823 +1639 +1981 +1763 +1758 +1819 +1569 +1580 +358 +1786 +1964 +1604 +1805 +1822 +1941 +1993 +1939 +1975 +1966 +1852 +1310 +1687 +1718 +641 +1715 +1995 +1603 +1444 +1641 +1961 +1536 +1771 +1267 +1749 +1944 +1519 +1445 +1818 +1558 +1922 +1452 +1901 +1915 +1957 +1840 +1785 +1946 +1683 +1918 +1847 +1690 +1716 +1627 +1571 +1985 +1455 +435 +1856 +1527 +1660 +1555 +1557 +1591 +1906 +1646 +1656 +1620 +1618 +1598 +1606 +1808 +1509 +1551 +1723 +1835 +1610 +1820 +1942 +1767 +1549 +1607 +1781 +1612 +1864 +2007 +1908 +1650 +1449 +1886 +1878 +1895 +1869 +1469 +1507 diff --git a/src/adventofcode2020.lisp b/src/adventofcode2020.lisp index 54a6aca..4eaba06 100644 --- a/src/adventofcode2020.lisp +++ b/src/adventofcode2020.lisp @@ -6,8 +6,8 @@ ,@body))) (defun part1 (str) - (format t "Part 1: ~A~%" str)) + (time (format t "Part 1: ~A~%" str))) (defun part2 (str) - (format t "Part 2: ~A~%" str)) + (time (format t "Part 2: ~A~%" str))) diff --git a/src/day01.lisp b/src/day01.lisp new file mode 100644 index 0000000..adb36e6 --- /dev/null +++ b/src/day01.lisp @@ -0,0 +1,29 @@ +(in-package #:adventofcode2020) + +(defun calculate-expenses (&rest reports) + (let ((prod (apply #'cartesian-product reports))) + (loop for tuple in prod + when (= (apply #'+ tuple) 2020) + return (apply #'* tuple)))) + +(day 01 input + (let ((report (int-list-from input))) + (part1 (calculate-expenses report report)) + (part2 (calculate-expenses report report report)))) + +(def-suite day01) +(in-suite day01) + +(test simple-expenses + (is (equal + 514579 + (let ((report '(1721 979 366 299 675 1456))) + (calculate-expenses report report))))) + +(test complex-expenses + (is (equal + 241861950 + (let ((report '(1721 979 366 299 675 1456))) + (calculate-expenses report report report))))) + +(run! 'day01) diff --git a/src/utilities.lisp b/src/utilities.lisp index c677e96..ab6c3e8 100644 --- a/src/utilities.lisp +++ b/src/utilities.lisp @@ -7,3 +7,34 @@ (defun int-list-from (str) (mapcar #'parse-integer (split "\\n" str))) + +(defun cartesian-product (A B &rest C) + (let ((helper (lambda (X Y) + (if Y (loop for x in X + nconc (loop for y in Y + collecting (append x (list y)))) + X))) + (wrap-A (mapcar #'list A))) + (reduce helper C :initial-value (funcall helper wrap-A B)))) + +(def-suite util) +(in-suite util) + +(test cart-prod-2 + (is (equal + '((a a) (a b) (b a) (b b)) + (cartesian-product '(a b) '(a b))))) + +(test cart-prod-3 + (is (equal + '((a a a) (a a b) (a b a) (a b b) + (b a a) (b a b) (b b a) (b b b)) + (cartesian-product '(a b) '(a b) '(a b))))) + +(test cart-prod-4 + (is (equal + '((a a a a) (a a a b) (a a b a) (a a b b) (a b a a) (a b a b) (a b b a) (a b b b) + (b a a a) (b a a b) (b a b a) (b a b b) (b b a a) (b b a b) (b b b a) (b b b b)) + (cartesian-product '(a b) '(a b) '(a b) '(a b))))) + + -- 2.26.2