added manhattan distance

This commit is contained in:
Peter Tillemans 2024-11-26 00:41:13 +01:00
parent cd1fd7f9c6
commit e999ee12de
5 changed files with 83 additions and 30 deletions

View file

@ -31,4 +31,4 @@
:components :components
((:file "main")))) ((:file "main"))))
:description "Test system for aoc" :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)))

View file

@ -1,14 +1,39 @@
(defpackage :aoc/2018/06 (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) (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) (x 0 :type fixnum)
(y 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) (defvar sample-data '((1 1)
(1 6) (1 6)
(8 3) (8 3)
@ -16,18 +41,21 @@
(5 5) (5 5)
(8 9))) (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) (defun make-vectors (data)
(let ((x-min (apply #'min (map 'list #'point-2d-x points) )) (map 'list (lambda (p) (make-vector-2d :x (first p) :y (second p))) data))
(y-min (apply #'min (map 'list #'point-2d-y points) )))
(make-point-2d :x x-min :y y-min)))
(defun bottom-right (points) (defvar sample-points (make-vectors sample-data))
(let ((x-max (apply #'max (map 'list #'point-2d-x points) )) (setf sample-points (make-vectors sample-data))
(y-max (apply #'max (map 'list #'point-2d-y points) )))
(make-point-2d :x x-max :y y-max))) (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)))

View file

@ -1,7 +1,8 @@
(defpackage aoc (defpackage aoc
(:use :cl) (:use :cl)
(:export (:export
#:test-input)) #:test-input
#:clear-data-cache))
(in-package :aoc) (in-package :aoc)
@ -35,6 +36,8 @@
:secure-p t)))) :secure-p t))))
(defun split-lines (s)
(cl-ppcre:split "\\n" s))
(defun fetch-input-data (year day) (defun fetch-input-data (year day)
@ -42,11 +45,14 @@
(multiple-value-bind (multiple-value-bind
(body) (body)
(dex:get url :cookie-jar *cookie-jar* :verbose t) (dex:get url :cookie-jar *cookie-jar* :verbose t)
body))) (split-lines body))))
(defvar *input-data-cache* (make-hash-table)) (defvar *input-data-cache* (make-hash-table))
(defun clear-data-cache ()
(setf *input-data-cache* (make-hash-table)))
(defun test-input (year day) (defun test-input (year day)
"Return input data for the given challenge. Use a cached value if already fetched" "Return input data for the given challenge. Use a cached value if already fetched"
(let* ((key (+ (* year 100) day)) (let* ((key (+ (* year 100) day))

View file

@ -1,16 +1,26 @@
(defpackage :aoc/2018/06 (defpackage :aoc/2018/06/tests
(:use :cl :aoc :parachute)) (: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 suite-2018-06)
(define-test+run find-top-left (define-test+run find-top-left
:parent suite-2018-06 :parent suite-2018-06
(let ((result (top-left sample-points))) (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 (define-test+run find-bottom-right
:parent suite-2018-06 :parent suite-2018-06
(let ((result (bottom-right sample-points))) (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)))
))

View file

@ -1,11 +1,20 @@
(defpackage aoc/tests/main (defpackage aoc/tests
(:use :cl (:use :cl :aoc :parachute
:aoc :aoc/2018/06 ))
:rove))
(in-package :aoc/tests/main) (in-package :aoc/tests)
;; NOTE: To run this test file, execute `(asdf:test-system :aoc)' in your Lisp. ;; NOTE: To run this test file, execute `(asdf:test-system :aoc)' in your Lisp.
(deftest test-target-1 ;; top of test suite tree
(testing "should (= 1 1) to be true" (define-test aoc-suite)
(ok (= 1 1))))
;; 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)))