From 37dc4d4e793d79845b35147d59401b7f3cbc5898 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Fri, 4 Dec 2020 00:02:45 -0500 Subject: [PATCH] Add day 04 (and fn library) --- adventofcode2020.asd | 2 + package.lisp | 15 +- res/day03 | 323 +++++++++++++++++++++++++++++++++++++++++++ src/day03.lisp | 57 ++++++++ 4 files changed, 391 insertions(+), 6 deletions(-) create mode 100644 res/day03 create mode 100644 src/day03.lisp diff --git a/adventofcode2020.asd b/adventofcode2020.asd index 96f4fbd..031f2e8 100644 --- a/adventofcode2020.asd +++ b/adventofcode2020.asd @@ -7,9 +7,11 @@ :serial t :depends-on (:alexandria :arrow-macros + :fn :fiveam) :pathname "src/" :components ((:file "package") + (:file "utilities") (:file "adventofcode2020")) :in-order-to ((test-op (test-op :adventofcode2020/test)))) diff --git a/package.lisp b/package.lisp index 6a6f94b..77c8922 100644 --- a/package.lisp +++ b/package.lisp @@ -1,8 +1,11 @@ -; (ql:quickload :fiveam) (ql:quickload :cl-ppcre) -(defpackage #:adventofcode2020 - (:use #:cl - #:arrow-macros - #:alexandria - #:fiveam) +(progn (ql:quickload :fiveam) + (ql:quickload :cl-ppcre) + (ql:quickload :fn)) +(defpackage :adventofcode2020 + (:use :cl + :arrow-macros + :alexandria + :fn + :fiveam) (:import-from :cl-ppcre :split) (:nicknames :aoc2020)) diff --git a/res/day03 b/res/day03 new file mode 100644 index 0000000..fd04fb1 --- /dev/null +++ b/res/day03 @@ -0,0 +1,323 @@ +....#...............##...#...#. +#...#..#.....##.##.#.##....#... +...#.....#...#................. +#..#..#.......#...#.#.......... +...##..#.#..........##...#..... +........###.#.##..#............ +...###......##.#..#.#...#.#.... +......##..#.#....#...#......... +.................#......#...... +..............##....#.......... +#.....................#...#.#.# +.##..#............##...##.##..# +.....#.####...#..##......#.#..# +#.......#.#..#......##.#.#....# +.....##...###.#..........##.... +#...........#.##....##.....#..# +..###..##.##.....#....#........ +...#.#.#............#.#..#....# +#......#....#...##.#.#.#.#..#.. +.......#.#...#..#..#..##......# +.....#..#.............#..#...#. +##..#.##.....#........#........ +....##....#..#...........#...#. +.......#........##.......##.... +..##...#.......#........##.#... +..........#..#.....##........#. +..#..##..#............#........ +.#.#...#...#.......#......#.... +....#....#.....#.#.........###. +.............#...#....#..#.#... +##.#...#..#......#.#.##.....#.. +#...##.#..........#..#.#...#... +#####.......#.#.....#..#....... +#...#...#........#....#...#.... +......##.#..#..#............#.. +....#....#.......#...###....... +.#......##...#.##....#...#..... +..#....#...##.....#.#...##.#... +#.......#........#.####........ +#.##..#..#.........#.#........# +.#...#.#.#.#......#....#.#..#.. +#...####...##.##.#....#......## +..#...#......##........#.....#. +...#.#....##................... +...##................#......... +...##.....##........#....#..#.. +.........#..#.....#............ +.#..#.......................#.. +.#.........#..##........#.#.#.# +......#.....##..#.##...#..#.##. +..#..............##.......#.... +...............##....#...##..#. +###...#..###.........#...#..... +...#..#...#....#.....##........ +....#..##...#........#......... +..#......#.......#.....#..#.... +.#...........##.....###....#... +.#..#.....##.........##.....#.. +....##.#.....#................# +..#..#......#.#..#....#..##.... +#.....#...##............#...... +.#.............#....#.......#.. +#.........#..#...##.#...#.#.##. +...#......#..####....#.#.....#. +......#........#..........##.## +......##.#..##.##.....#........ +##.....#..##.##..#.......##.... +.##.........#...........#...#.. +.....#...###..#...#...........# +..........#.#......#.###.....#. +...#.............#.##......##.. +#.##.........#..###...........# +....#..##....#..#..#........### +...#........##.......##..#..#.. +...#......#..#.#............... +#......###....#.#..#.#..#....#. +#.#.####.#.........#..#.#.#.... +.....#....#...............#...# +.#........#......#.#...#....... +................#...#.....##... +.............#...####.......... +.................##....##.###.. +#................#......#...... +.###.#........#..##.....#..###. +..#.#..#...#..#.#...#.#.....#.# +.....#............#..##..#..#.# +#........##.#...#.....#........ +#.#.#..###......###............ +...#..#...........##...#.....#. +......#........#...#.#....#.... +....#..........#.#..#.#....##.. +...#.....##..#......#.#.##...#. +.........#..#................#. +..#....#.##.....#.......#...... +...#.....#.......##.##.....#... +#...#..............#..###..#... +#.#......#.#....#........##..#. +...#...##...##..#.........#.... +..#...#......##.#.#.#....##.... +#.......#.......#..#..#........ +.........#..#.....#....#.....## +.#......#.......#.#..#..#...#.# +..#....#.#..#.................. +#.....####..........#.#.....#.# +.#..#.#.#....#.#.....#.#....... +....##......#..#.....#.#.#...#. +...##...#......##.#....##.#.... +..#..##....#...#...........#... +.......#........#...##.#....... +#.#..#....##.#....##........... +.......#............#..##..##.. +#.#.#.....#....##.#.#.#.....#.. +##...#...#.......#..#...#.....# +##..##.##..........#........##. +..............#.....#..#..##... +.......#...#.........#....#.#.. +...#..#..#....#.#....##........ +..#.......#....#....##......... +#...#.....#..#.#...##....#..... +.....##..#..##..#.............. +.....##............#....#.#.... +..#.....#....##.#.....#..#..... +#...#..#..#......#.#.#..##..... +.............................## +#...#.#................#....#.# +.#.#.#....##......###..##...... +#.....#..#.##.#.#.##...###..... +.........#............##..#.... +.#..#...#....#.....#.#........# +...............#......#..#..... +...................###........# +.###..##..##.......#.#......... +#.........#......#....#.#...#.. +.#.#....#.......#.#..##...##... +.#.....#....##.......#.#.....#. +.........#...#....#.#.......... +....###..#..##.#...##....#..#.. +...#.#..##.#.........###.#..#.. +#...#...........#....#......... +....##...........#.#.#......### +#....#...........##..#......... +###....#.....#.......#....###.. +.#.......#....#.#.#.#......#.#. +........#...............#.#.#.. +....#.........#.....#...##.##.# +...#............#.............# +..........#..#................. +........#.....##............#.# +..#...##........#...#.....#.#.. +....#........#.#.#..........#.. +#.#...#...........#............ +....#.#...##...........#.....#. +...........#.#..#.....#........ +.....#..#..#..#.....#.#.....#.# +#.....#.......#.......#...#.... +#.........#....#.#........#..#. +...#..#.........#.....#..#..... +...#..#.............#.......... +.#.......#..........#.....#...# +.....#.#......#.......#....#... +...#.....#..#..##....##....#... +.#.#.#..#...#.....#....#....... +..##.#..........#.....#.#...... +..#..#.............#...##..##.. +.#.............#..#....##...#.. +..#...#.....#.................# +..##.......#.....#...#....#.... +.#..#.##.........#...#.#...#... +...##.......##..#.....##.##...# +........####.#.........#....... +..#.#...##.#..#..#.......##.#.. +.#..#............###..#..#..... +#.....#.#...#.#.......#........ +..........#......#.#...#...#... +..#......#..#..#.#...#......... +..###........#.#....#.#...##... +.#.....#..#.#......#........#.. +.#...#..#...#....#.......#..#.. +..#....#..#.....#.#........#... +#..#.#.........#..........#..#. +.#.....##....#.........#.#.#.#. +#.#...#.....#.#.#....#.#..#.... +.........#...................#. +..#.....#..##...#..........#.#. +..............#....#.........#. +.#....#.....#..............##.. +#...#...#.#........##.........# +....###....#.#....#.#.........# +.....#........#.....##......... +.#...##..##..#.........##...... +............#.....#........#... +..#....#.......#......#..#.#.#. +#.......#.#...........#..##.#.. +......#.##......#....#.......#. +.....#........#...###.....#.... +###..........#........#.#.#.... +.....#...#.#...#...#...##.....# +.##...#.#........#.#....#...... +......#.........#.....#.#...... +.....#.##.....###.#...#...##..# +.#.#.......##....#..#..#.##.... +.####...###.#.#.#.#............ +......#..##...#..........#.##.# +......#............#........... +.....#.#..#.......##...##...... +......#........#..#....#.#.#.#. +#..#..#.....#..#.....#.......#. +.#...#.....#..............#.... +.#....#..#.##.#............#### +..........#....#.##...#.#...... +...#.#.#.#.#.......#.........#. +##........#..##..#.........#... +..#......#...#..#.#.....#...... +..#.#......#...#...#.#......... +........................##..... +...#.##.#........#...#.......#. +..#.#......#....##........#.#.. +#......#.##........#..#......#. +.....#..#..#.............#..... +......#......#........#....#... +...#....###.....#..#.#....#.... +#.......................#....#. +..#...#...................#.... +....#..#.....##.#..#...#.....#. +...#.........#...#.......#..... +..#....#.....#...#...#.#####... +.....####......#...........#... +......#.#..........#...#.#.#..# +###..#.#....#..#............... +...#...###..#..#.#.#........... +.....#...#.##.#.#.###..##...... +.........#...........#....##.#. +....#..#......#................ +...........#..#..#...#.#....... +..#.....#......##.###.......... +.........#...................#. +..........#...#.#....##........ +..#...##....#....#.......#...## +#......#.....#...#...#...#..... +....##...#.#.......#.#...##.... +...#.....#....#.....#....#..... +#....##.....##..##..........##. +.....#.....#.#.#............... +.#.##....#.....#.#..#....#..##. +.....#.#.....##....#........... +.........#..#.......##..##..... +..#....##.....###...#....#.#... +............#......#.#...#..#.. +#..##......#.#.##....#.#....... +.#.#.....#...#.#.#....#.....#.. +#....#..#.#....#...#........... +......#.#.....#...#.#.#......#. +###..#....#.###.............#.. +..............#####........###. +..#..#.#.#.#......#......#..... +###.........#.#..........#..#.# +.#.........#...#......####..... +..#.......####..#....#...#..#.. +#.#..#.#...............#.#.#.#. +###....#.....##.#....#......##. +..#..#........#....###.#.#..... +...#.#..........#.....#...#.... +....#......##.#............#..# +...##...#.....#..##....#..#.#.# +.......#.....#..#....#....##.#. +.#..#....#..#......##....##...# +..#......#...#.#..###..#.##.... +#...#.....#......##...#.......# +.....#.#.....#...##............ +.#..##.##..#..##.#........#.... +....#.#......##...#.#.#.#..##.. +.#..............##........#.... +.##....#..#..#....#...#......#. +............###....##.......##. +..............####.....#....... +........##..##.#...#.......#... +....#..#.....##.......#####...# +.##..##..#.....#...#..#..#....# +##..#.#.#...........#.......... +#..#......#...#....#........... +...#..##.#..........#..#....... +........#.#.....#......##...... +.....#....#............#....... +.#.#..#....##......#.......###. +.#..#.#........#......#...##..# +...#....#......#..#........#.## +.........#..#...#..#.#.##...... +....###.#...........#...#...... +.##............#.......#..##... +##...#.#...............#.#...## +..#..#.....#.#..#..#........... +..#..#.##..#......#.##..#.#.... +..#...#......#.#...#....##.#... +...###....####......#....#...#. +.......##........#.....##....#. +.........##..........#...#..... +.....#............#.##.#....#.# +..........#...#....##.......... +....................#......#... +#......#..#...#.............##. +...........#................... +..#...#.........#.##.#..##.#... +#.#....#.#.....#............#.. +.#..#.....#.....####......#.#.. +#....#.......##..#...........#. +............#...#.....#..#.#... +#...........#...#####....#...#. +..........#...###..##.........# +#.....###............#..#..#.#. +...##.....#....#......#.....#.. +#....#.......#..#......###...#. +...##.##......##..##..........# +.......#.#..#.#..#.#.#.#..#..#. +..#..###...#....#.....#......#. +...#.........#..#.##.#.....###. +..#.........#.##.#..#..#..###.. +..####..#.........#.........#.# +..#.#...#.......#....##........ +.#......#.#.................... +..........#.......#.#..#..#.... +..#........#....#.#..#......... +..#.....#.............#....#... +##...#.........#.....#...#..... diff --git a/src/day03.lisp b/src/day03.lisp new file mode 100644 index 0000000..6cd0743 --- /dev/null +++ b/src/day03.lisp @@ -0,0 +1,57 @@ +(in-package #:adventofcode2020) + +(defun tree-collisions (slope tree-map) + (destructuring-bind (rise run) slope + (loop with terrain-width = (-> tree-map first length) + for i upfrom 0 by run + for j upfrom 0 + for terrain-line in tree-map + when (->> rise (mod j) (= 0)) + counting (->> terrain-width + (mod i) + (char terrain-line) + (char= #\#))))) + +(day 03 input + (let ((tree-map (list-from input))) + (part1 (tree-collisions '(1 3) tree-map)) + (part2 (->> '((1 1) (1 3) (1 5) (1 7) (2 1)) + (mapcar (fn* (tree-collisions _ tree-map))) + (reduce #'*))))) + +(def-suite day03) +(in-suite day03) + +(test single-collision-check + (is (equal + 7 + (tree-collisions '(1 3) '("..##......." + "#...#...#.." + ".#....#..#." + "..#.#...#.#" + ".#...##..#." + "..#.##....." + ".#.#.#....#" + ".#........#" + "#.##...#..." + "#...##....#" + ".#..#...#.#"))))) + + +(test multiple-collision-check + (is (equal + '(2 7 3 4 2) + (mapcar (fn* (tree-collisions _ '("..##......." + "#...#...#.." + ".#....#..#." + "..#.#...#.#" + ".#...##..#." + "..#.##....." + ".#.#.#....#" + ".#........#" + "#.##...#..." + "#...##....#" + ".#..#...#.#"))) + '((1 1) (1 3) (1 5) (1 7) (2 1)))))) + +(run! 'day03) -- 2.38.5