1.rkt 717 B

1234567891011121314151617181920212223242526272829
  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 (point card)
  16. (define wins (length (filter (λ (x) (member x (card 'win-nums))) (card 'nums))))
  17. (if (= wins 0)
  18. 0
  19. (expt 2 (- wins 1))))
  20. (apply + (map point (map parse-card lines)))