2.rkt 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #lang racket
  2. (require "../lib/utils.rkt")
  3. (require "../lib/obj.rkt")
  4. (define fp (open-input-file "input"))
  5. (define lines (get-lines fp))
  6. (define (strip-head s)
  7. (string-trim (cadr (string-split s ":"))))
  8. (set! lines (map strip-head lines))
  9. (define make-card (obj-maker 'win-nums 'nums))
  10. (define (parse-card s)
  11. (define lst (string-split s "|"))
  12. (define win-nums (map string->number (string-split (car lst))))
  13. (define nums (map string->number (string-split (cadr lst))))
  14. (make-card win-nums nums))
  15. (define cards (map parse-card lines))
  16. (define card-count (make-vector (length cards) 1))
  17. (define (win-count card)
  18. (length (filter (λ (x) (member x (card 'win-nums))) (card 'nums))))
  19. (define card-vec (list->vector cards))
  20. (let loop ((i 0))
  21. (if (>= i (vector-length card-count))
  22. (void)
  23. (let ()
  24. (define win-cnt (win-count (vector-ref card-vec i)))
  25. (let loop ((j (+ i 1)))
  26. (if (or (>= j (vector-length card-count))
  27. (>= j (+ 1 i win-cnt)))
  28. (void)
  29. (let ()
  30. (vector-set! card-count j (+ (vector-ref card-count i)
  31. (vector-ref card-count j)))
  32. (loop (+ 1 j)))))
  33. (loop (+ 1 i)))))
  34. (apply + (vector->list card-count))