Advertisement
Guest User

ADVENT OF CODE DAY 1

a guest
Dec 1st, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 3.23 KB | None | 0 0
  1. (use srfi-1);;a hacky way to get emacs to recognize chicken files
  2. (define DIRECTIONS
  3.   '((R . 4) (R . 5) (L . 5) (L . 5) (L . 3) (R . 2) (R . 1) (R . 1) (L . 5)
  4.     (R . 5) (R . 2) (L . 1) (L . 3) (L . 4) (R . 3) (L . 1) (L . 1) (R . 2)
  5.     (R . 3) (R . 3) (R . 1) (L . 3) (L . 5) (R . 3) (R . 1) (L . 1) (R . 1)
  6.     (R . 2) (L . 1) (L . 4) (L . 5) (R . 4) (R . 2) (L . 192) (R . 5) (L . 2)
  7.     (R . 53) (R . 1) (L . 5) (R . 73) (R . 5) (L . 5) (R . 186) (L . 3) (L . 2)
  8.     (R . 1) (R . 3) (L . 3) (L . 3) (R . 1) (L . 4) (L . 2) (R . 3) (L . 5)
  9.     (R . 4) (R . 3) (R . 1) (L . 1) (R . 5) (R . 2) (R . 1) (R . 1) (R . 1)
  10.     (R . 3) (R . 2) (L . 1) (R . 5) (R . 1) (L . 5) (R . 2) (L . 2) (L . 4)
  11.     (R . 3) (L . 1) (R . 4) (L . 5) (R . 4) (R . 3) (L . 5) (L . 3) (R . 4)
  12.     (R . 2) (L . 5) (L . 5) (R . 2) (R . 3) (R . 5) (R . 4) (R . 2) (R . 1)
  13.     (L . 1) (L . 5) (L . 2) (L . 3) (L . 4) (L . 5) (L . 4) (L . 5) (L . 1)
  14.     (R . 3) (R . 4) (R . 5) (R . 3) (L . 5) (L . 4) (L . 3) (L . 1) (L . 4)
  15.     (R . 2) (R . 5) (R . 5) (R . 4) (L . 2) (L . 4) (R . 3) (R . 1) (L . 2)
  16.     (R . 5) (L . 5) (R . 1) (R . 1) (L . 1) (L . 5) (L . 5) (L . 2) (L . 1)
  17.     (R . 5) (R . 2) (L . 4) (L . 1) (R . 4) (R . 3) (L . 3) (R . 1) (R . 5)
  18.     (L . 1) (L . 4) (R . 2) (L . 3) (R . 5) (R . 3) (R . 1) (L . 3)))
  19.  
  20. (define (rotdir-l dir)
  21.   ;;dir is a cons cell: (x . y)
  22.   (if (= (car dir) 0)
  23.       (cons (- (cdr dir)) 0)
  24.       (cons (cdr dir) (car dir))))
  25.  
  26. (define (rotdir-r dir)
  27.   (if (= (cdr dir) 0)
  28.       (cons 0 (- (car dir)))
  29.       (cons (cdr dir) (car dir))))
  30.  
  31. (define (add-to-point p dir n)
  32.   ;;I am way prouder of this than I should be
  33.   (cons (+ (car p)
  34.            (* (car dir) n))
  35.         (+ (cdr p)
  36.            (* (cdr dir) n))))
  37.  
  38. (define (new-direction curdir rsym)
  39.   ((if (equal? rsym 'R) rotdir-r rotdir-l) curdir))
  40.  
  41. (define (range from to)
  42.   (let loop ((from from)
  43.              (to ((if (< from to) - +) to 1))
  44.              (lst '()))
  45.     (if (= from to)
  46.         (cons to lst)
  47.         (loop from ((if (< from to) - +) to 1) (cons to lst)))))
  48.  
  49. (define (path-to from to)
  50.   (if (= (car to) (car from))
  51.       (map (lambda (x) (cons (car to) x)) (range (cdr from) (cdr to)))
  52.       (map (lambda (x) (cons x (cdr to))) (range (car from) (car to)))))
  53.  
  54. (define (get-path dirlist initdr)
  55.   ;;returns a list of all locations traversed. The car is the final location
  56.   (let loop ((dirlist dirlist)
  57.              (dir initdr)
  58.              (curloc '(0 . 0))
  59.              (visited '()))
  60.     (cond ((null? dirlist) (append (path-to curloc (car visited)) visited))
  61.           (else (let ((newdir (new-direction dir (caar dirlist))))
  62.                   (loop (cdr dirlist) newdir
  63.                         (add-to-point curloc newdir (cdar dirlist))
  64.                         (if (null? visited)
  65.                             (list curloc)
  66.                             (append (path-to curloc (car visited))
  67.                                     visited))))))))
  68.  
  69. (define (find-first-intersec locs)
  70.   ;;you have to REVERSE the output of get-path before you put it in here.
  71.   (let loop ((locs locs)
  72.              (visited '()))
  73.     (cond
  74.      ((null? locs) #f)
  75.      ((member (car locs) visited) (car locs))
  76.      (else (loop (cdr locs) (cons (car locs) visited))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement