2.rkt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. (define (pairing l)
  9. (define (loop ret l)
  10. (if (null? l)
  11. ret
  12. (loop
  13. (cons (cons
  14. (car l)
  15. (+ (car l) (cadr l)))
  16. ret)
  17. (cddr l))))
  18. (loop '() l))
  19. (reverse
  20. (pairing (map string->number (string-split nums-str " "))))))
  21. (begin (read-line port) (void))
  22. (define (read-line-convert-eof port)
  23. (define line (read-line port))
  24. (if (eof-object? line) "" line))
  25. (define (read-map)
  26. (define (loop ret)
  27. (define line (string-trim (read-line-convert-eof port)))
  28. (if (= 0 (string-length line))
  29. (sort (reverse ret) (λ (x y) (< (cadr x) (cadr y))))
  30. (loop (cons (map string->number (string-split line " "))
  31. ret))))
  32. (read-line port)
  33. (loop '()))
  34. (define s2s (read-map))
  35. (define s2f (read-map))
  36. (define f2w (read-map))
  37. (define w2l (read-map))
  38. (define l2t (read-map))
  39. (define t2h (read-map))
  40. (define h2l (read-map))
  41. (define maps (list s2s s2f f2w w2l l2t t2h h2l))
  42. (define (range-map mlist r)
  43. ;; r :: Range :: (start . end)
  44. ;; mlist :: List of (Map :: (dest, src, len))
  45. (define (loop result mlist r)
  46. (define start (car r))
  47. (define end (cdr r))
  48. (if (null? mlist)
  49. (cons r result)
  50. (let ()
  51. (define cur-map (car mlist))
  52. (define map-target (car cur-map))
  53. (define map-start (cadr cur-map))
  54. (define map-end (+ map-start (caddr cur-map)))
  55. (define offset (- map-target map-start))
  56. (define (pair-offset p) (cons (+ offset (car p)) (+ offset (cdr p))))
  57. (cond ((<= end start)
  58. result)
  59. ((>= start map-end)
  60. (loop result (cdr mlist) r))
  61. ((>= start map-start)
  62. (loop
  63. (cons (pair-offset (cons start (min end map-end)))
  64. result)
  65. (cdr mlist)
  66. (cons (min end map-end) end)))
  67. ((<= end map-start)
  68. (cons r result))
  69. ((< start map-start)
  70. (loop
  71. (cons (cons start map-start) result)
  72. mlist
  73. (cons map-start end)))
  74. (else (error "unhandled cond in range-map"))))))
  75. (reverse (loop '() mlist r)))
  76. (define (gen-range-mapper the-map)
  77. (define (mapper range-list)
  78. (apply append
  79. (map
  80. (λ (range)
  81. (range-map the-map range))
  82. range-list)))
  83. mapper)
  84. (define mappers (map gen-range-mapper maps))
  85. (define (comp-func funcs)
  86. (define procs (reverse funcs))
  87. (define (comp-rec arg)
  88. (if (null? procs)
  89. arg
  90. (let ((proc (car procs))
  91. (rest (cdr procs)))
  92. (set! procs rest)
  93. (proc (comp-rec arg)))))
  94. comp-rec)
  95. (define (find-location x)
  96. ((comp-func mappers) x))
  97. (display (apply min (map car (find-location seed))))