From 7164df2132241c0b150794030508ef7d6515fd32 Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Sun, 1 Dec 2024 20:06:41 +0100 Subject: [PATCH] solved day 7 of 2018 --- src/2018/day07.lisp | 98 ++++++++++++++++++++++++++++++++++---- src/main.lisp | 18 +++++++ tests/2018/day07-test.lisp | 25 ++++++++++ 3 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 tests/2018/day07-test.lisp diff --git a/src/2018/day07.lisp b/src/2018/day07.lisp index 04648b9..d71ac47 100644 --- a/src/2018/day07.lisp +++ b/src/2018/day07.lisp @@ -1,5 +1,10 @@ (defpackage :aoc/2018/07 - (:use :cl :aoc :alexandria :queues)) + (:use :cl :aoc :alexandria :queues) + (:export + #:sample-data + #:part1 + #:step-time + )) (in-package :aoc/2018/07) (defparameter sample-input @@ -37,14 +42,91 @@ Step F must be finished before step E can begin.")) if (not (member p produced)) collect p)))) +(defun available-steps (done dependencies) + (remove-if + (lambda (x) (member x done)) + (append + (steps-without-dependencies dependencies) + (loop for dep in dependencies + if (and + (member (first dep) done) + (prerequisites-availablep done (second dep) dependencies)) + collect (second dep))))) + +(defun next-step (done dependencies) + (let ((available (available-steps done dependencies))) + (first (sort available #'string-lessp)))) + +(defun assembly-order (dependencies &optional (done '())) + (let ((next (next-step done dependencies))) + (if (not next) + (reverse done) + (assembly-order dependencies (push next done))))) + +(defun part1 (&optional (data input-data)) + (format nil "~{~A~}" (assembly-order data))) + +;;; ------------------------------------------------------------------- + +(defun step-time (step &optional (offset 60)) + (+ offset (mod (char-code (character step)) 32))) + +(defun available-timed-steps (time done dependencies &optional (offset 60)) + (map 'list + (lambda (s) (list (+ time (step-time s offset)) s)) + (available-steps (map 'list #'second done) dependencies))) + +(defun timed-step-lessp (a b) + (cond + ((eq (first a) (first b)) (string-lessp (second a) (second b))) + (t (< (first a) (first b))))) + +(defun merge-timed-steps (steps add workers) + (let ((new-steps steps)) + (loop for ev in (sort add #'timed-step-lessp) + while (< (length new-steps) workers) + if (not (member (second ev) (mapcar #'second new-steps))) + do (setf new-steps (push ev new-steps))) + (sort new-steps #'timed-step-lessp))) + +(defun assembly-order-timed (dependencies &key (time 0) (done '()) (todo '()) (offset 60) (workers 5)) + + ;; done is a sorted list of (ts step) pairs + + (format t "todo: ~A with ~A at ~A~%" todo done time) + (cond + ((and (not done) (not todo)) + (assembly-order-timed dependencies + :time time + :done done + :todo (available-timed-steps time done dependencies offset) + :offset offset + :workers workers)) + ((and done (not todo)) + (progn + (format t "we're done : ~A~%" done) + done)) + (t + (let* ((next-done (append done (remove-if (lambda (x) (< time (first x))) todo))) + (next-todo (merge-timed-steps + (remove-if (lambda (x) (>= time (first x))) todo) + (available-timed-steps time next-done dependencies offset) + workers))) + (format t "next todo: ~A, done: ~A ~%" next-todo next-done) + (let* ((next-step (first next-todo)) + (nt (first next-step))) + (format t "~A~%" next-todo) + (assembly-order-timed + dependencies + :time nt + :done next-done + :todo next-todo + :offset offset + :workers workers)))))) - - - - - - - +(defun part2 () + (let ((finish (first (first (last (assembly-order-timed input-data)))))) + (format nil "~A" finish))) diff --git a/src/main.lisp b/src/main.lisp index ee75f98..539c26d 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -3,6 +3,8 @@ (:export #:split-lines #:test-input + #:submit-part1 + #:submit-part2 #:clear-data-cache)) (in-package :aoc) @@ -49,6 +51,22 @@ (split-lines body)))) +(defun submit-answer (year day level answer) + (let ((url (format nil "~A/~D/day/~D/answer" *aoc-url* year day))) + (multiple-value-bind + (body) + (dex:post url :cookie-jar *cookie-jar* :verbose t + :content `(("level" . ,level) + ("answer" . ,answer) + ("submit" . "[Submit]"))) + body))) + +(defun submit-part1 (year day answer) + (submit-answer year day "1" answer)) + +(defun submit-part2 (year day answer) + (submit-answer year day "2" answer)) + (defvar *input-data-cache* (make-hash-table)) (defun clear-data-cache () diff --git a/tests/2018/day07-test.lisp b/tests/2018/day07-test.lisp new file mode 100644 index 0000000..2d5110b --- /dev/null +++ b/tests/2018/day07-test.lisp @@ -0,0 +1,25 @@ + +(defpackage :aoc/2018/07/tests + (:use :cl :aoc :aoc/tests :parachute :aoc/2018/07) + (:export + #:suite-2018-07 + #:test-part1 + #:test-step-size + )) + +(in-package :aoc/2018/07/tests) + +(define-test suite-2018-07 + ;:parent suite-2018 + ) + + +(define-test test-part1 + :parent suite-2018-07 + (true (equal "CABDFE" (part1 sample-data)))) + +(define-test test-step-size + :parent suite-2018-07 + (true (equal 63 (step-time 'C))) + (true (equal 3 (step-time 'C 0))) + )