From 14b3da78938ea914991b62f7525dd9fbe19e9f8b Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Sun, 1 Dec 2024 12:04:03 +0100 Subject: [PATCH] add files I've been working on. --- src/2018/day07.lisp | 100 +++++++++++++++++++++++++++++++++++++ src/2024/day01.lisp | 45 +++++++++++++++++ tests/2018/day06-test.lisp | 33 ++++++++++++ tests/2024/day01-test.lisp | 19 +++++++ tests/2024/main.lisp | 9 ++++ 5 files changed, 206 insertions(+) create mode 100644 src/2018/day07.lisp create mode 100644 src/2024/day01.lisp create mode 100644 tests/2018/day06-test.lisp create mode 100644 tests/2024/day01-test.lisp create mode 100644 tests/2024/main.lisp diff --git a/src/2018/day07.lisp b/src/2018/day07.lisp new file mode 100644 index 0000000..98a5f5d --- /dev/null +++ b/src/2018/day07.lisp @@ -0,0 +1,100 @@ +(defpackage :aoc/2018/07 + (:use :cl :aoc :alexandria :queues)) +(in-package :aoc/2018/07) + +(defparameter sample-input + (sb-unicode:lines "Step C must be finished before step A can begin. +Step C must be finished before step F can begin. +Step A must be finished before step B can begin. +Step A must be finished before step D can begin. +Step B must be finished before step E can begin. +Step D must be finished before step E can begin. +Step F must be finished before step E can begin.")) + +(defun parse-line (s) + (let ((words (sb-unicode:words s))) + (list + (read-from-string (nth 2 words)) + (read-from-string (nth 14 words))))) + +(defvar sample-data '()) +(setf sample-data (map 'list #'parse-line sample-input)) + +(defvar input-data '()) +(setf input-data (map 'list #'parse-line (aoc:test-input 2018 7))) + +(defun prerequisites-availablep (available target dependencies) + (every (lambda (s) (member s available)) + (loop for dep in dependencies + if (eq target (second dep)) + collect (first dep)))) + +(defun steps-without-dependencies (dependencies) + (let ((produced (loop for dep in dependencies collect (second dep)))) + (remove-duplicates + (loop for dep in dependencies + for p = (first dep) + if (not (member p produced)) + collect p)))) + +(defun dependency-lessp (a b) + (string-lessp (second a) (second b))) + +(defun next-ready-rules (dependencies done) + (let ((available (append done (steps-without-dependencies dependencies)))) + (sort + (loop for d in dependencies + if (member (first d) available) + if (not (member (second d) available)) + if (prerequisites-availablep available (second d) dependencies) + collect d) + #'dependency-lessp))) + + +(defun merge-steps (steps rule) + (if (member (first rule) steps) + (append steps (list (second rule))) + (append steps rule))) + +(defun number-steps (dependencies) + (length + (remove-duplicates + (append + (map 'list #'first dependencies) + (map 'list #'second dependencies))))) + +(defparameter *counter* 0) +(setf *counter* 0) + +(defparameter *path* '()) + +(defun assy-order (dependencies) + (let ((todo (queues:make-queue :simple-queue)) + (target-length (number-steps dependencies)) + (path '())) + (loop for step in (steps-without-dependencies dependencies) + do (queues:qpush todo (list step))) + (queues:print-queue todo) + (loop + do (setf path (queues:qpop todo)) + do (setf *path* path) + do (incf *counter*) + while (and path (not (= target-length (length path)))) + do (loop + for rule in (next-ready-rules dependencies path) + for tgt = (second rule) + for npath = (append path (list tgt)) + do (queues:qpush todo npath))) + path)) + + + + +(defun print-thread-info () + (let* ((curr-thread sb-thread:*current-thread*) + (curr-thread-name (sb-thread:thread-name curr-thread)) + (all-threads (sb-thread:list-all-threads))) + (format t "Current thread: ~a~%~%" curr-thread) + (format t "Current thread name: ~a~%~%" curr-thread-name) + (format t "All threads:~% ~{~a~%~}~%" all-threads)) + nil) diff --git a/src/2024/day01.lisp b/src/2024/day01.lisp new file mode 100644 index 0000000..27cda02 --- /dev/null +++ b/src/2024/day01.lisp @@ -0,0 +1,45 @@ +(defpackage :aoc/2024/01 + (:use :cl :aoc :alexandria) + (:export + #:sample-data + #:part1 + #:part2)) + +(in-package :aoc/2024/01) + + +(defun parse-line (s) + (map 'list #'parse-integer (cl-ppcre:split " " s))) + +(defvar input-data '()) +(setf input-data (map 'list #'parse-line (test-input 2024 1))) + +(defvar sample-text "") +(setf sample-text (aoc:split-lines "3 4 +4 3 +2 5 +1 3 +3 9 +3 3")) + +(defvar sample-data '()) +(setf sample-data (map 'list #'parse-line sample-text)) + +(defun difference (a b) + (abs (- b a))) + +(defun part1 (data) + (reduce #'+ + (mapcar #'difference + (sort (mapcar #'first data) #'<) + (sort (mapcar #'second data) #'<)))) + +(defun part2 (data) + (let ((left (mapcar #'first data)) + (right (mapcar #'second data))) + (loop for l in left + sum (* l (loop for r in right count (= l r)))))) + +(defun solve-day () + (format t "part1: ~A~%" (part1 input-data)) + (format t "part2: ~A~%" (part2 input-data))) diff --git a/tests/2018/day06-test.lisp b/tests/2018/day06-test.lisp new file mode 100644 index 0000000..22ccd8b --- /dev/null +++ b/tests/2018/day06-test.lisp @@ -0,0 +1,33 @@ +(defpackage :aoc/2018/06/tests + (:use :cl :aoc :aoc/tests :parachute :aoc/2018/06) + (:export + #:suite-2018-06 + #:test-find-top-left + #:test-find-bottom-right + #:test-manhattan-distance + )) + +(in-package :aoc/2018/06/tests) + +(define-test suite-2018-06 + :parent suite-2018) + +(define-test+run test-find-top-left + :parent suite-2018-06 + (let ((result (top-left sample-points))) + (true (equalp (make-vector-2d :x 1 :y 1) result)))) + +(define-test+run test-find-bottom-right + :parent suite-2018-06 + (let ((result (bottom-right sample-points))) + (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/2024/day01-test.lisp b/tests/2024/day01-test.lisp new file mode 100644 index 0000000..2f71b1b --- /dev/null +++ b/tests/2024/day01-test.lisp @@ -0,0 +1,19 @@ + +(defpackage :aoc/2024/01/tests + (:use :cl :aoc :aoc/tests :aoc/2024/tests :parachute :aoc/2024/01)) + +(in-package :aoc/2024/01/tests) + +(define-test suite-2024-01 + ;:parent suite-2024 + ) + +(define-test+run test-part1 + :parent suite-2024-01 + (true (equalp 11 (part1 sample-data)))) + +(define-test+run test-part2 + :parent suite-2024-01 + (true (equalp 31 (part2 sample-data)))) + + diff --git a/tests/2024/main.lisp b/tests/2024/main.lisp new file mode 100644 index 0000000..b1fa81b --- /dev/null +++ b/tests/2024/main.lisp @@ -0,0 +1,9 @@ +(defpackage :aoc/2024/tests + (:use :cl :aoc/tests :parachute) + (:export + #:suite-2024)) + +(in-package :aoc/2024/tests) + +(define-test suite-2024 + :parent 'aoc-suite)