diff options
-rwxr-xr-x | day19 | 30 | ||||
-rw-r--r-- | day19.mew | 45 | ||||
-rw-r--r-- | day19.rkt | 58 |
3 files changed, 133 insertions, 0 deletions
diff --git a/day19 b/day19 new file mode 100755 index 0000000..5537c4e --- /dev/null +++ b/day19 @@ -0,0 +1,30 @@ +Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 11 clay. Each geode robot costs 3 ore and 8 obsidian. +Blueprint 2: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 14 clay. Each geode robot costs 3 ore and 16 obsidian. +Blueprint 3: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 15 clay. Each geode robot costs 3 ore and 9 obsidian. +Blueprint 4: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 15 clay. Each geode robot costs 2 ore and 8 obsidian. +Blueprint 5: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 8 clay. Each geode robot costs 2 ore and 18 obsidian. +Blueprint 6: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 4 ore and 20 obsidian. +Blueprint 7: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 6 clay. Each geode robot costs 2 ore and 20 obsidian. +Blueprint 8: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 4 ore and 10 obsidian. +Blueprint 9: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 15 clay. Each geode robot costs 4 ore and 16 obsidian. +Blueprint 10: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 19 clay. Each geode robot costs 4 ore and 12 obsidian. +Blueprint 11: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 3 ore and 17 obsidian. +Blueprint 12: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 19 clay. Each geode robot costs 2 ore and 12 obsidian. +Blueprint 13: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 16 clay. Each geode robot costs 4 ore and 17 obsidian. +Blueprint 14: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 9 obsidian. +Blueprint 15: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 17 clay. Each geode robot costs 3 ore and 7 obsidian. +Blueprint 16: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 4 ore and 9 obsidian. +Blueprint 17: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 3 ore and 8 obsidian. +Blueprint 18: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 11 clay. Each geode robot costs 2 ore and 7 obsidian. +Blueprint 19: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 18 clay. Each geode robot costs 2 ore and 19 obsidian. +Blueprint 20: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 4 ore and 8 obsidian. +Blueprint 21: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 9 clay. Each geode robot costs 3 ore and 7 obsidian. +Blueprint 22: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 3 ore and 12 obsidian. +Blueprint 23: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 3 ore and 19 obsidian. +Blueprint 24: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 16 clay. Each geode robot costs 3 ore and 9 obsidian. +Blueprint 25: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 9 clay. Each geode robot costs 4 ore and 16 obsidian. +Blueprint 26: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 12 clay. Each geode robot costs 2 ore and 10 obsidian. +Blueprint 27: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 8 clay. Each geode robot costs 3 ore and 19 obsidian. +Blueprint 28: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 3 ore and 9 obsidian. +Blueprint 29: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 11 obsidian. +Blueprint 30: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 13 clay. Each geode robot costs 2 ore and 20 obsidian. diff --git a/day19.mew b/day19.mew new file mode 100644 index 0000000..4ea42cb --- /dev/null +++ b/day19.mew @@ -0,0 +1,45 @@ +(def blueprints + (for/into '() (line (lines "day19")) + (match (for/into '() (n (gdrop (gmatch "[0-9]+" line) 1)) + (string->number n)) + ((a b c d e f) `(((0 0 0 ,a) (0 0 0 1)) + ((0 0 0 ,b) (0 0 1 0)) + ((0 0 ,d ,c) (0 1 0 0)) + ((0 ,f 0 ,e) (1 0 0 0)) + ((0 0 0 0) (0 0 0 0))) + )))) + +(def ((list-compare cmp) x y) + (rep comparing (x x y y) + (cond ((null? x) #f) ; same + ((cmp (car x) (car y)) #t) + ((cmp (car y) (car x)) #f) + (else (comparing (cdr x) (cdr y)))))) + +(def (score> a b) + ((list-compare >) (app map + a) (app map + b))) + +(define (run blueprint t limit) + (loc (todo '(((0 0 0 0) (0 0 0 1)))) + (for (n (range 0 t)) + (set todo + (=> (accumulate (new-todo '()) + (for ((have make) todo) + (for ((cost more) blueprint) + (when (every >= have cost) + (new-todo (list (map + have make (map - cost)) + (map + make more))))))) + (op sort _ score>) + gen + (op gtake _ limit) + (op into '() _)))) + (sort! todo score>) + (first todo))) + +(prn (for/inject + (b blueprints i (range 1)) + (* i (caar (run b 24 500))))) +;; 1294 + +(prn (for/inject * (b blueprints i (range 0 3)) + (caar (run b 32 2000)))) +;; 13640 diff --git a/day19.rkt b/day19.rkt new file mode 100644 index 0000000..4034754 --- /dev/null +++ b/day19.rkt @@ -0,0 +1,58 @@ +#lang racket + +(define blueprints + (for/list ([line (file->lines "day19s")]) + (match (cdr (map string->number (regexp-match* #rx"[0-9]+" line))) + [(list a b c d e f) + (list (list (list 0 0 0 a) (list 0 0 0 1)) + (list (list 0 0 0 b) (list 0 0 1 0)) + (list (list 0 0 d c) (list 0 1 0 0)) + (list (list 0 f 0 e) (list 1 0 0 0)) + (list (list 0 0 0 0) (list 0 0 0 0)))]))) + +(define (list>? x y) + (let comparing ((x x) (y y)) + (cond ((null? x) #f) ; same + ((> (car x) (car y)) #t) + ((> (car y) (car x)) #f) + (else (comparing (cdr x) (cdr y)))))) + +(define (key a) + (map + (list-ref a 0) (list-ref a 1))) + +(define (take* lst n) + (if (< (length lst) n) + lst + (take lst n))) + +(define (run blueprint t) + (let ([todo (list (list (list 0 0 0 0) (list 0 0 0 1)))]) + (for ([_ (in-range 0 t)]) + (let ([new-todo '()]) + (for* ([item1 todo] + [item2 blueprint]) + (let ([have (first item1)] + [make (second item1)] + [cost (first item2)] + [more (second item2)]) + (when (andmap >= have cost) + (set! new-todo (cons (list (map + have make (map - cost)) + (map + make more)) + new-todo))))) + (set! todo (take* (sort new-todo list>? #:key key) 2000)))) + (first (sort todo list>? #:key key)))) + +; (pretty-print blueprints) + +(run (first blueprints) 24) +(run (second blueprints) 24) + +(for/sum ([b blueprints] + [i (in-inclusive-range 1 (length blueprints))]) + (* i (caar (run b 24)))) +;; 1294 + +(for/product ([b blueprints] + [i (in-inclusive-range 1 3)]) + (caar (run b 32))) +;; 13640 |