Advertisement
Guest User

Untitled

a guest
Aug 8th, 2014
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #lang racket
  2. (require syntax/parse/define)
  3.  
  4. (define-simple-macro (recognizer ((n ...) ...))
  5. #:with (((x-offset y-offset) ...) ...) (for/list ([(foo i) (in-indexed (syntax->list #'((n ...) ...)))])
  6. (for/list ([(bar j) (in-indexed (syntax->list foo))])
  7. #`(#,j #,i)))
  8. (λ (arr x y)
  9. (and (and (eq? n (vector-ref (vector-ref arr (+ y y-offset))
  10. (+ x x-offset)))
  11. ...)
  12. ...)))
  13.  
  14. (define recs
  15. (list (recognizer [[1 1 1] [1 0 1] [1 0 1] [1 0 1] [1 1 1]])
  16. (recognizer [[1 1 0] [0 1 0] [0 1 0] [0 1 0] [1 1 1]])
  17. (recognizer [[1 1 1] [0 0 1] [1 1 1] [1 0 0] [1 1 1]])
  18. (recognizer [[1 1 1] [0 0 1] [1 1 1] [0 0 1] [1 1 1]])
  19. (recognizer [[1 0 1] [1 0 1] [1 1 1] [0 0 1] [0 0 1]])
  20. (recognizer [[1 1 1] [1 0 0] [1 1 1] [0 0 1] [1 1 1]])
  21. (recognizer [[1 1 1] [1 0 0] [1 1 1] [1 0 1] [1 1 1]])
  22. (recognizer [[1 1 1] [0 0 1] [0 1 1] [0 1 0] [0 1 0]])
  23. (recognizer [[1 1 1] [1 0 1] [1 1 1] [1 0 1] [1 1 1]])
  24. (recognizer [[1 1 1] [1 0 1] [1 1 1] [0 0 1] [1 1 1]])))
  25.  
  26. (define (to-bits str)
  27. (list->vector (map (compose string->number list->string list)
  28. (string->list str))))
  29.  
  30. (define bits (list->vector (map to-bits (file->lines (command-line #:args (filename) filename)))))
  31.  
  32. (define y-l (vector-length bits))
  33. (define x-l (vector-length (vector-ref bits 0)))
  34.  
  35. (time (for/hash ([(r n) (in-indexed recs)])
  36. (values n (for*/list ([i (in-range 0 (- y-l 4))]
  37. [j (in-range 0 (- x-l 2))]
  38. #:when (r bits j i))
  39. (cons i j)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement