1.rkt 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #lang racket
  2. (define port (open-input-file "input"))
  3. (define seed
  4. (let ()
  5. (define nums-str
  6. (string-trim
  7. (list-ref (string-split (read-line port) ":") 1)))
  8. (map string->number (string-split nums-str " "))))
  9. (begin (read-line port) (void))
  10. (define (read-line-convert-eof port)
  11. (define line (read-line port))
  12. (if (eof-object? line) "" line))
  13. (define (read-map)
  14. (define (loop ret)
  15. (define line (string-trim (read-line-convert-eof port)))
  16. (if (= 0 (string-length line))
  17. (reverse ret)
  18. (loop (cons (map string->number (string-split line " "))
  19. ret))))
  20. (read-line port)
  21. (loop '()))
  22. (define s2s (read-map))
  23. (define s2f (read-map))
  24. (define f2w (read-map))
  25. (define w2l (read-map))
  26. (define l2t (read-map))
  27. (define t2h (read-map))
  28. (define h2l (read-map))
  29. (define maps (list s2s s2f f2w w2l l2t t2h h2l))
  30. (define (gen-mapper the-map)
  31. (define (mapper x)
  32. (define (loop the-map)
  33. (if (null? the-map)
  34. x
  35. (let ()
  36. (define cur-map (car the-map))
  37. (define target (car cur-map))
  38. (define start (cadr cur-map))
  39. (define len (caddr cur-map))
  40. (if (and (>= x start)
  41. (<= x (+ start len)))
  42. (+ target (- x start))
  43. (loop (cdr the-map))))))
  44. (loop the-map))
  45. mapper)
  46. (define mappers (map gen-mapper maps))
  47. (define (comp-func funcs)
  48. (define procs (reverse funcs))
  49. (define (comp-rec arg)
  50. (if (null? procs)
  51. arg
  52. (let ((proc (car procs))
  53. (rest (cdr procs)))
  54. (set! procs rest)
  55. (proc (comp-rec arg)))))
  56. comp-rec)
  57. (define (find-location x)
  58. ((comp-func mappers) x))
  59. (display (apply min (map find-location seed)))