diff --git a/aoc.asd b/aoc.asd index 28db69e..7ae8af8 100644 --- a/aoc.asd +++ b/aoc.asd @@ -31,4 +31,4 @@ :components ((:file "main")))) :description "Test system for aoc" - :perform (test-op (op c) (symbol-call :rove :run c))) + :perform (test-op (op c) (symbol-call :parachute :test :aoc/tests))) diff --git a/src/2018/day06.lisp b/src/2018/day06.lisp index 005d118..5ea9bf3 100644 --- a/src/2018/day06.lisp +++ b/src/2018/day06.lisp @@ -1,14 +1,39 @@ (defpackage :aoc/2018/06 - (:use :cl :aoc)) + (:use :cl :aoc) + (:export + #:vector-2d + #:make-vector-2d + #:vector-2d-x + #:vector-2d-y + #:vector-2d-sub + #:manhattan-distance + #:sample-points + #:top-left + #:bottom-right)) (in-package :aoc/2018/06) -(defvar input-data (test-input 2018 6)) +(defun parse-line (s) + (map 'list #'parse-integer (cl-ppcre:split ", " s))) -(defstruct point-2d +(defvar input-data '()) +(setf input-data (map 'list #'parse-line (test-input 2018 6))) + +(defstruct vector-2d (x 0 :type fixnum) (y 0 :type fixnum)) +(defun vector-2d-sub (p1 p2) + (make-vector-2d + :x (- (vector-2d-x p1) (vector-2d-x p2)) + :y (- (vector-2d-y p1) (vector-2d-y p2)))) + +(defun manhattan-distance (p1 p2) + (let ((diff (vector-2d-sub p1 p2))) + (+ + (abs (vector-2d-x diff)) + (abs (vector-2d-y diff))))) + (defvar sample-data '((1 1) (1 6) (8 3) @@ -16,18 +41,21 @@ (5 5) (8 9))) -(defun make-points (data) - (map 'list (lambda (p) (make-point-2d :x (first p) :y (second p))) data)) -(defvar sample-points (make-points sample-data)) -(defun top-left (points) - (let ((x-min (apply #'min (map 'list #'point-2d-x points) )) - (y-min (apply #'min (map 'list #'point-2d-y points) ))) - (make-point-2d :x x-min :y y-min))) +(defun make-vectors (data) + (map 'list (lambda (p) (make-vector-2d :x (first p) :y (second p))) data)) -(defun bottom-right (points) - (let ((x-max (apply #'max (map 'list #'point-2d-x points) )) - (y-max (apply #'max (map 'list #'point-2d-y points) ))) - (make-point-2d :x x-max :y y-max))) +(defvar sample-points (make-vectors sample-data)) +(setf sample-points (make-vectors sample-data)) + +(defun top-left (vectors) + (let ((x-min (apply #'min (map 'list #'vector-2d-x vectors) )) + (y-min (apply #'min (map 'list #'vector-2d-y vectors) ))) + (make-vector-2d :x x-min :y y-min))) + +(defun bottom-right (vectors) + (let ((x-max (apply #'max (map 'list #'vector-2d-x vectors) )) + (y-max (apply #'max (map 'list #'vector-2d-y vectors) ))) + (make-vector-2d :x x-max :y y-max))) diff --git a/src/main.lisp b/src/main.lisp index 604e6d7..19a0033 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -1,7 +1,8 @@ (defpackage aoc (:use :cl) (:export - #:test-input)) + #:test-input + #:clear-data-cache)) (in-package :aoc) @@ -35,6 +36,8 @@ :secure-p t)))) +(defun split-lines (s) + (cl-ppcre:split "\\n" s)) (defun fetch-input-data (year day) @@ -42,11 +45,14 @@ (multiple-value-bind (body) (dex:get url :cookie-jar *cookie-jar* :verbose t) - body))) + (split-lines body)))) (defvar *input-data-cache* (make-hash-table)) +(defun clear-data-cache () + (setf *input-data-cache* (make-hash-table))) + (defun test-input (year day) "Return input data for the given challenge. Use a cached value if already fetched" (let* ((key (+ (* year 100) day)) diff --git a/test/2018/day06-test.lisp b/test/2018/day06-test.lisp index 1d21af8..f43b145 100644 --- a/test/2018/day06-test.lisp +++ b/test/2018/day06-test.lisp @@ -1,16 +1,26 @@ -(defpackage :aoc/2018/06 - (:use :cl :aoc :parachute)) +(defpackage :aoc/2018/06/tests + (:use :cl :aoc :aoc/2018/06 :parachute)) -(in-package :aoc/2018/06) +(in-package :aoc/2018/06/tests) (define-test suite-2018-06) (define-test+run find-top-left :parent suite-2018-06 (let ((result (top-left sample-points))) - (true (equalp (make-point-2d :x 1 :y 1) result)))) + (true (equalp (make-vector-2d :x 1 :y 1) result)))) (define-test+run find-bottom-right :parent suite-2018-06 (let ((result (bottom-right sample-points))) - (true (equalp (make-point-2d :x 8 :y 9) result)))) + (true (equalp (make-vector-2d :x 8 :y 9) result)))) + +(define-test test-manhattan-distance + :parent suite-2018-06 + (let ((p0 (make-vector-2d :x 0 :y 0)) + (p1 (make-vector-2d :x 1 :y 1)) + (p2 (make-vector-2d :x 3 :y 2))) + (true (eq 0 (manhattan-distance p0 p0))) + (true (eq 2 (manhattan-distance p0 p1))) + (true (eq 5 (manhattan-distance p0 p2))) + )) diff --git a/tests/main.lisp b/tests/main.lisp index 43d9d34..c4b3a60 100644 --- a/tests/main.lisp +++ b/tests/main.lisp @@ -1,11 +1,20 @@ -(defpackage aoc/tests/main - (:use :cl - :aoc - :rove)) -(in-package :aoc/tests/main) +(defpackage aoc/tests + (:use :cl :aoc :parachute + :aoc/2018/06 )) + +(in-package :aoc/tests) ;; NOTE: To run this test file, execute `(asdf:test-system :aoc)' in your Lisp. -(deftest test-target-1 - (testing "should (= 1 1) to be true" - (ok (= 1 1)))) +;; top of test suite tree +(define-test aoc-suite) + + +;; suite for every year +(define-test suite-2018 + :parent aoc-suite) + + +(define-test "should (= 1 1) to be true" + :parent aoc-suite + (true (= 1 1)))