about summary refs log tree commit diff
path: root/day19.rkt
blob: 4034754482adfadfd2d041a3b27cd9244e00e84e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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