~theothornhill/advent

02eee8b1d8f4ed9fd88128ad93f07b0f008645f3 — Theodor Thornhill 4 years ago 54d58d9
Day 3 complete
5 files changed, 409 insertions(+), 2 deletions(-)

A 2020/days/day3.lisp
A 2020/inputs/day3.txt
M 2020/package.lisp
M 2020/tests/2020-tests.lisp
M advent.asd
A 2020/days/day3.lisp => 2020/days/day3.lisp +72 -0
@@ 0,0 1,72 @@
(in-package #:advent-2020)

;; Only global vars mode

(defvar *day3-file* "2020/inputs/day3.txt")

(defparameter *trees* 0)
(defparameter *i* 0)
(defparameter *j* 0)
(defparameter *area* (apply #'vector (slurp *day3-file*)))
(defparameter *height* 322)
(defparameter *width* 30)
(defparameter *current-j-steps* 0)
(defvar *tree-sign* #\#)
(defvar *j-steps* '(1 3 5 7 1))
(defvar *i-steps* '(1 1 1 1 2))

(defparameter *j-step* 3)
(defparameter *i-step* 1)
(defvar *reset* 0)

(defparameter *round* 0)
(defvar *max-rounds* 5)

(defparameter *round-results* nil)

(defun crashed-in-tree-p ()
  (string= (aref (aref *area* *i*) *j*) *tree-sign*))

(defun walk ()
  (tagbody
   :again
     (if (= *j* *width*) (setf *j* *reset*) (incf *j*))
     (incf *current-j-steps*)
     (if (> *current-j-steps* (1- *j-step*)) (go :done) (go :again))
   :done)
  (incf *i* *i-step*)
  (when (crashed-in-tree-p) (incf *trees*))
  (setf *current-j-steps* *reset*))

(defun crashed-in-tree-part-1 ()
  (setf *j-step* 3 *i-step* 1)
  (tagbody
   :walk-again
     (walk)
     (if (= *i* *height*) (go :done) (go :walk-again))
   :done)
  *trees*)

;; part 2

(defun crashed-in-tree-part-2 ()
  (tagbody
   :initialize
     (setf *round* *reset*)
     (go :set-new-steps)
   :set-new-steps
     (setf *trees* *reset*)
     (setf *i-step* (nth *round* *i-steps*)
           *j-step* (nth *round* *j-steps*))
     (incf *round*)
     (go :walk)
   :walk
     (walk)
     (if (= *i* *height*) (go :maybe-go-again) (go :walk))
   :maybe-go-again
     (push *trees* *round-results*)
     (setf *i* *reset*)
     (setf *j* *reset*)
     (if (= *round* *max-rounds*) (go :done) (go :set-new-steps))
   :done)
  (apply #'* *round-results*))

A 2020/inputs/day3.txt => 2020/inputs/day3.txt +323 -0
@@ 0,0 1,323 @@
....#...............#.#..###.##
.#..#....###..............##...
....###......#....#.#...#.##..#
.......#........#..###...##....
.....#..#......#..#..##..#...#.
....#..........#....#...#......
............###...#............
##......#.....#......#.....##..
........#.........##..#.#...##.
....#.#..#.#...#........#..#...
.#.....#.#......#....#..#..#..#
#.##..##......#.....##...#..#..
#........#..##...###....##.....
......#.#..##...#.#.....#......
##.......#..#.........#...#....
.....##.........#....#.#.###.#.
..##...........#.#.#.#.....#.#.
....#...............#......#.#.
#.#..#....#.....#.....##...#..#
#......#..............#.#.##...
......###.....#...#........###.
####...#.....#...#....#........
.......#...#....##...#.........
.####..##............#.........
#.#...#...#....#...#.#......#..
..#..#.....#.......#...#.#...##
.#.........#...#......##.#...#.
.#.#...#...#.....#.#........#.#
.#.....###....###..##.#..##.#..
.....##....#......#..#...#...#.
#...##....#.......#.....##.##..
#...#.....#.#...........#..###.
##.#........###...........###.#
#...#.#........#.#.....#.......
..................#..#.........
.....#.#..#.#......#..#.....##.
.#.#.......#..##........#..##.#
.#.#..#.#...#.......#.#.#..#...
...#......#....#....##.#..#....
......#.......##....##..#.....#
...#.##...##...............#..#
.###....#.#.....##..#.......#.#
#....#..........#...........#.#
...#...............#.#..#....#.
.....#..##..........#..###.....
.....####.....#.#.......#...#..
#.............#...#.......##...
.#....##.......#.#......#.#.##.
.#..#.......#..##...#...#......
#.......#..#..#..#.....#.......
##...#.#.#...........#....#....
.......#..#.#..............#.#.
.....#.......#.......#.#.#.....
....##.##.....#......#.......#.
#...#..#.#....###....##...#.#.#
#..#......#........#.#.#.....#.
###..##..#......#.....#.......#
..##....#.#.#......#..##...#...
.....#..#....#...#.#...#...#...
.....#.#..###.#..#...##......#.
#.#..#....#..#.....#.#...#.#...
.#..#....#.......#..#.......#..
#.........#..#..#.........##..#
..##.##..#..#...##.............
.....###...#..#...##.#..#......
#.##.....##..............#.....
.......#.##.#.##...#.#.......##
...#.#.##...#......#...........
##.#........#.....##.....#.....
.#.....#.............#......#..
....#..##..#..#....#..#.#......
.#.....#....##..##..#...##.....
.##........#.#.#.#..........#.#
...#.#.#..#....#...#..###.##...
.#....#....#.#.#.#....#..#.....
#.#.......#..#..#...........#.#
.....#.....##..#....##.........
....#.##..............#........
.................#....#.......#
...................###...#...#.
...#.#..#..##..##....#.....#...
#...#..........................
.......#..#..#.#..#.....#......
..##.#..#......#...#.##..##..#.
.##.........#.#...........#....
...#...#..##.#......#..#..#....
.....#.#....#...#............#.
.##..#.....##....#...#.........
#......##...#...#............#.
.....#.##...#.#....##..........
.............#.......#.#.......
##....#.#........#....#..##....
....#...##.#....##..#.....#.#..
...##..#....##......#...#......
.####.#..#..#.#...#.#.#....#...
.#........#.##....#.#....#.....
.........#....##..#..#.........
....##...#....##.............#.
....#..##.#....#.#..#...##.....
.....##...#..#....#......#.#...
..........#.......#.##..#.##..#
.......#.........#...#.##......
....##.#.......#...............
....#.......#..##.......##.#.##
#.#..#.#....#.#.........###...#
.#.##.....##..##...........#.#.
...#....##........##...#...#...
.#.##....#.#...#.#..#..#...#...
#....#.##...#.#..#....#.....#..
#..#...#........#...........#..
...........#.......#......#..#.
....##...#......##.....#......#
.#.##.#.#.............#....##..
.#...#...##.#.#........#..##.#.
.#.#........#.#...#..#........#
.###.#.#...#..#..#.#....#..#...
..##..##....#.#............#...
#..........#........#..#.#.#...
.#...#..#..#.#.....#.....#....#
#.....#.#.#.....####.......#...
...#.#........#.#.###...#.....#
.....#.....##....#....#..##...#
..#....###.##.#..#..#....#...#.
.....#.....#...........#.#.###.
.....#......###...#.#...##.....
...........###..#...#....#.#..#
...#..###.....#....#.#...#.....
.......#...#..#..##....#.#.#...
...#..####.###........#.....#..
..#......###..#..#.##..........
....#....##..##..##.......###..
...#.......#..#.#....##........
.#.#.....#.#.#..........#..#..#
.##....##....##...........##.##
........#......#.##....##...#.#
..#.#.....#..#....#..........#.
...........#...............#...
.....####.....#.....###.#..#...
..........####..##.##.#....#...
...#.#.#......#....#..#.#......
.#.#......###.....#....#.......
..#..#..#.......#..#...#.....#.
...#............#......##...###
......#.............#..#..###..
.#.#......#..##.#..#..#.#...#.#
.#.....#.......#..#...........#
..#.###.#..#...#.##..#.##......
....#.#........#..#.#...#.#.##.
.#..##.#..#.#.#...##..#.#......
.......#..#..#..#.....#.#.#..#.
.##.###..##.....#.##..#........
...##..............#.#.##....#.
##....#...#...........#........
..#........#.#.##..#.#...#..#..
.......#.......##.#..#....#.#..
.......#....##..#.#.#..#....#..
..........#....#..#..#....#....
........#.....#.#.....##.......
........##.###.........#.#.#...
###......####...#.#........#...
......#........#......#.....#..
##..#.##..##.###..#........##..
.#..#...#............##.##..#..
...........#..#.#..............
.##.#.#....#...............#..#
.........##.................#..
#............##..##.........##.
##........#.........#..##.#...#
........#.....#..#.........#.##
....#......#.....##.##.........
.#.#.....#.#..#..##....#....#..
.###...#..##....##.....#.#..##.
.#....#.#.......#..#..#...###..
..#.#......#.#..#.....###.....#
#....#..#...#.....#.......#.##.
.#.......##.#.#...#......#.....
###....#.#......#....#.#...##..
...#....####.......##.....#..#.
.#.................#.......##.#
...#.###..........#..##......#.
.....##.#..............##..#...
#.....#..#..........#..#.......
..#...#.#.#.......##.#.....#...
#........#.#........#.#.....#..
#.....#...##....##..##........#
.#.##..#...#....#........#..#..
#..#.....#....#...##......#....
...#...#...#.#.#....##....#.#.#
......#...............#.....#..
.......#.#..#..##.#.....#......
...........##......#...#.......
....##..##.....#.#...#.........
......##..###.......#....#.##..
......#..#.##....#..###..#.....
.....#.........#........##.....
.....####..................#...
.#.#...##................#.....
.#..#...#...#.....#.##..#..#...
.#................#...###....#.
...#....#...........#...#....#.
.......#....##...............#.
.#.#.##..##.......#....#....###
......#.#....#...#..#..........
....#.##.#.#...##.#.#......#...
##....#...##.....#..#.###.#....
.......#......#.........#.#...#
.##.#...........##.........#.#.
##..##.....#...#..#.#...#....#.
#..##......###........#...#....
.....#.#.......#...#..##....##.
.....#.#..#.....#.......#..##..
...#..#..............#.#.......
.#.#...###......###............
.....#.....#...#.###...........
.......#..................#...#
#....#...#...#....#....#.#....#
....#..#............#.#........
#....#..........#.#.#..#..#....
.......#....#......#....#......
.##.#.#...#...#...##...........
.........###.#..#..............
...#........##....#....#....###
....##..#.......#.#...#.#..#.#.
.....##....#..##.........#.....
........##..#........#.........
...........#..#.##..##...#.....
.........#.#..####..#...#.##.##
##..#.#.....##.....#.........#.
..#...#...#....#.#....#.....#.#
.###.#....#.#......#....#......
.##.....#....#.......#.#..#.##.
#..#..##.....#....#...##.....#.
...#.........####.........##..#
..#.....#....###.#.#...#..#....
.........#....#..#.#.........#.
.....###.##..#...#.....#..#..#.
....#......#..#.#...#.....#....
.......#...#..#....#.......#.#.
.#....#............#....####...
#..##..##....#.....#...#.....#.
...#...##...#.#....#...........
.......#####.....#..###.#..#...
.....#.....#.#....#.........#..
.###.#..#...##.##.#.#..#..#....
.#..#.##..#......#..........##.
##....#....#.........###..#....
..#.............#.......#.#....
..#.....#..##...#...###..#.##..
##...##...#.#....#..#..........
...............#.....#.......#.
....#.#......##.#......#...#.#.
.........#.............#.#.....
...#.#.........................
..#..#....##..#...###.##.......
...##.#...##..#.#.##.#...#.##..
.##....#....#.......#.....##..#
.#...............#..#...#......
...##.....##.###....#.....#...#
...#.....#...####....##....#..#
..#.#.###..##.....#........#...
.....##.##..#..#.........##....
........##.....#..........#.##.
..##.#....####..#...........#..
##....#..#..#.#.##.....#...##..
...#...#......#..#.#....#......
##.....##.......##.##....#.....
.........#...##...........#....
.###.#..#....##...#.....#.....#
...#..........#.###..##...#.##.
...#..#....#.#.#.......###.....
....#..#.#.....##...#.#.#.#....
.......##..#...##..##.#....#...
.##..........#.#.#.....#.....#.
#....#......##...#..##.#..#..#.
.#...#.....###..#......#.....#.
.#..#.###.......#.##....###....
#....#....#....#....#..#.##....
..#..#.....#.....#....###.....#
##.###..#...##.......#.#.......
#...##......##....#.#...#....#.
..##.#.#....#...#.....##.......
.#....#..#...#...##..##........
###......#.##....#.#..##.......
...#.....##.#.........#..#.....
.......#.#....#.....##......#..
#..#..............##.#........#
....#.#....#..#.#...##.........
..........#..........#.........
.#.....#.....#.##....#.##..#..#
.......#.......#.#.#.##....#...
..#...........#....#.......##..
..#.#.#.#...##..#.#.#..#...#.#.
..#..#..........#...##....#....
....##.#....#.............#....
.##...##..........#.#..#...#...
#..#..#.##..........##.........
............#.......#..#.....##
....#......#........#..#.##....
#.#......#.#...#.....#.........
..#.....#..#..........#.....#..
.#..#.#.#.##...#..#.#.........#
#...##....#..##..#...#.#.##....
#..##.##.#.##.......#.......#..
#.#.......#........#.##....#.#.
....#..##....##..##......#..##.
#.....#....#.#........####.....
......#...#...###..#...........
.##.#.##...##................##
..##.#.....#.#..#......#.#.....
......#...#........#.....#.##..
#..#.#..#.....#.#..#..##..#.#..
...#.........#.#.#.##...#......
...#..##....#..#.#....#.###.#..
........###................##.#
##...........#......##.##.....#
.#.#.#....#....#....#..........
#.....#........................
....#.....#...#......#.........
....#.#..#..............#......
##.........#..#....#.......#...
.###....#..#.#.####.........#..
..#.#....#.....###..#..........
..........#................#.##

M 2020/package.lisp => 2020/package.lisp +3 -1
@@ 4,4 4,6 @@
   :is-it-2020?-part-1
   :is-it-2020?-part-2
   :count-valid-passwords-part-1
   :count-valid-passwords-part-2))
   :count-valid-passwords-part-2
   :crashed-in-tree-part-1
   :crashed-in-tree-part-2))

M 2020/tests/2020-tests.lisp => 2020/tests/2020-tests.lisp +9 -0
@@ 16,3 16,12 @@
(deftest day2-part-2
  (testing "Day two part 2 should return correct number"
    (ok (= (count-valid-passwords-part-2) 737))))


(deftest day3-part-1
  (testing "Day three part 1 should return correct number"
    (ok (= (crashed-in-tree-part-1) 211))))

(deftest day3-part-2
  (testing "Day three part 2 should return correct number"
    (ok (= (crashed-in-tree-part-2) 3584591857))))

M advent.asd => advent.asd +2 -1
@@ 19,7 19,8 @@
                             (:file "utils")
                             (:module "days"
                              :components ((:file "day1")
                                           (:file "day2"))))))
                                           (:file "day2")
                                           (:file "day3"))))))
  :in-order-to ((test-op (test-op "advent/tests"))))

(asdf:defsystem :advent/tests