at least part 1 of day 19

This commit is contained in:
efim
2022-12-24 12:45:54 +00:00
parent bf52645d93
commit 2eb4b5c0a5
5 changed files with 216 additions and 15 deletions

View File

@@ -344,22 +344,15 @@
(defmethod find-max-geod-2 (blueprints (s state))
;; (declare (optimize (debug 3)))
(format t "in step for ~a; with ~a~%" (minute s) s)
(when (and
(not (cur-found-max s)) ; first pruning max
(is-satiated-p blueprints s))
(setf (cur-found-max s) s))
;; (format t "in step for ~a; with ~a~%" (minute s) s)
(cond
((= 25 (minute s)) ; exit condition fully calculated
(getf (resources s) :geode 0))
((and (cur-found-max s) (a-dominates-b-p blueprints (cur-found-max s) s)
0)) ; pruning this branch
((< (estimate s) (cur-found-max s))
;; (print "pruning")
0) ; pruning this branch
(t ; default check
(progn
(when (and (cur-found-max s)
(a-dominates-b-p blueprints s (cur-found-max s)))
(setf (cur-found-max s) s)) ; set current state as domineering
(let* ((will-collect-this-minute (calc-resources-to-be-collected s))
(possible-bot-builds (get-possible-bot-builds blueprints s))
(max-if-building
@@ -377,15 +370,93 @@
;; (break)
(add-resources will-collect-this-minute state-copy)
(incf (minute state-copy))
(find-max-geod-2 blueprints state-copy ))))
(find-max-geod-2 blueprints state-copy )))
(recursed-max (max (or max-if-building 0) if-not-building)))
;; (break)
;; (format t "would build ~a~%" possible-bot-builds)
(max (or max-if-building 0) if-not-building))))))
(when (> recursed-max (cur-found-max s))
(setf (cur-found-max s) recursed-max))
recursed-max
)))))
(setq *test-state* (make-instance 'state))
(setf (cur-found-max *test-state*) nil)
(print (find-max-geod-2 *test-blueprint* *test-state*))
(setf (cur-found-max *test-state*) 0)
(timing (print (find-max-geod-2 *test-blueprint* *test-state*)))
(timing (let ((a 1) (b 2)) (* a b 15)))
;; thank you CL-Cookbook: https://cl-cookbook.sourceforge.net/dates_and_times.html
(defmacro timing (&body forms)
(let ((real1 (gensym))
(real2 (gensym))
(run1 (gensym))
(run2 (gensym))
(result (gensym)))
`(let* ((,real1 (get-internal-real-time))
(,run1 (get-internal-run-time))
(,result (progn ,@forms))
(,run2 (get-internal-run-time))
(,real2 (get-internal-real-time)))
(format *debug-io* ";;; Computation took:~%")
(format *debug-io* ";;; ~f seconds of real time~%"
(/ (- ,real2 ,real1) internal-time-units-per-second))
(format t ";;; ~f seconds of run time~%"
(/ (- ,run2 ,run1) internal-time-units-per-second))
,result)))
;; so, why doesn't it ever builds obsidian?
*test-blueprint*
;; because is "has to build something if it can" whops
;; wow 20 seconds. cool
;; last things:
;; read in the line for the blueprints into plist
;; then loop over lines in file
;; for each line compute max, multiply by index
;; and sum
;; ok, i guess
(rest (remove-if-not #'identity
(mapcar (lambda (str) (parse-integer str :junk-allowed t))
(ppcre:split " "
"Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian."))))
(blueprint-line-to-plist "Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.")
;; and now loop over file.
(print (with-open-file (in "day19-test.txt")
(loop
for line = (read-line in nil nil)
for n from 1
for blueprints = (when line (progn
(format t "Starting processing for ~a~%" line)
(blueprint-line-to-plist line)))
for max-geo = 0
;; for max-geo = (when blueprints
;; (progn
;; (setf (cur-found-max *test-state*) 0)
;; (timing (find-max-geod-2 blueprints (make-instance 'state)))))
while blueprints
do (format t "processed ~a : ~a. its max is ~a~%" n blueprints max-geo)
summing (* n max-geo))))
(format t "and the result is : ~a~%" (read-and-calc-part-1 "day19-test.txt"))
(format t "and the result is : ~a~%" (read-and-calc-part-1 "day19-input.txt"))
;; wtf is taking so long in between the processings?
(apply #'* (list 2 3 5))
;; but before that - change exit point
(format t "and the result is : ~a~%" (read-and-calc-part-2 "day19-2-input.txt"))
;; 261 is too low. so
;; but in previous my maxes were 0 1 2
;; and current are 3 3 29
;; but still not good
;; ugh. what about test input?
(format t "and the result is : ~a~%" (read-and-calc-part-2 "day19-test.txt"))
;; my calc : 18 and 55
;; correct : 56 and 62
;; coooool. let's move on.