solved day 7 of 2018

This commit is contained in:
Peter Tillemans 2024-12-01 20:06:41 +01:00
parent f3ceb796b6
commit 7164df2132
3 changed files with 133 additions and 8 deletions

View file

@ -1,5 +1,10 @@
(defpackage :aoc/2018/07 (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) (in-package :aoc/2018/07)
(defparameter sample-input (defparameter sample-input
@ -37,14 +42,91 @@ Step F must be finished before step E can begin."))
if (not (member p produced)) if (not (member p produced))
collect p)))) 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)))

View file

@ -3,6 +3,8 @@
(:export (:export
#:split-lines #:split-lines
#:test-input #:test-input
#:submit-part1
#:submit-part2
#:clear-data-cache)) #:clear-data-cache))
(in-package :aoc) (in-package :aoc)
@ -49,6 +51,22 @@
(split-lines body)))) (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)) (defvar *input-data-cache* (make-hash-table))
(defun clear-data-cache () (defun clear-data-cache ()

View file

@ -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)))
)