PasteRack.org
Paste # 92103
2015-12-02 17:33:53

Forked from paste # 53413.

Fork as a new paste.

Paste viewed 449 times.


Embed:

AoC, 1

  1. #lang racket
  2.  
  3. (define (fold-str-int f i str)
  4.   (sequence-fold f i (sequence-map char->integer (in-string str))))
  5.  
  6. (define (elevator str)
  7.   (inexact->exact
  8.    (* (fold-str-int
  9.        (lambda (i c)
  10.          (+ i (- c 40.5))) 0 str) -2)))
  11.  
  12. (define (disallow-basement str)
  13.   (letrec
  14.       ([make-counter
  15.         (lambda ([count 0] [v 0])
  16.           (lambda (c)
  17.             (let
  18.                 ([v (+ v (- c 40.5))]
  19.                  [count (+ 1 count)])
  20.               (if (< 0 v)
  21.                   (raise-user-error (~a count))
  22.                   (make-counter count v)))))])
  23.     (inexact->exact
  24.      (* (fold-str-int (lambda (f c) (f c)) (make-counter 0 0) str) -2))))
  25.  
  26. (display (elevator ")())())")) (newline)
  27. (disallow-basement "()())")

=>

-3

5