Skip to content

Instantly share code, notes, and snippets.

@taylskid
Created December 4, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taylskid/6cbed3d707712cf154848fd71dba0ec7 to your computer and use it in GitHub Desktop.
Save taylskid/6cbed3d707712cf154848fd71dba0ec7 to your computer and use it in GitHub Desktop.
(struct pos (x y) #:transparent)
(define (pos+ p1 p2)
(match-let ([(pos x1 y1) p1]
[(pos x2 y2) p2])
(pos (+ x1 x2)
(+ y1 y2))))
(define (flip f)
(lambda (x y)
(f y x)))
(define (gen-points start-pos dir dis)
(let loop ([cur start-pos]
[dis dis]
[points null])
(if (zero? dis)
points
(let ([new (pos+ cur dir)])
(loop new (sub1 dis) (cons new points))))))
(define (calculate-segment-points line)
(let loop ([line line]
[cur-pos (pos 0 0)]
[segment (set)])
(if (empty? line)
segment
(let* ([dir (match (caar line)
['R (pos 1 0)]
['U (pos 0 1)]
['L (pos -1 0)]
['D (pos 0 -1)])]
[points (gen-points cur-pos dir (cadar line))])
(loop (cdr line)
(car points)
(set-union segment
(list->set points)))))))
(define (parse-line line)
(map (lambda (x) (list (string->symbol (substring x 0 1))
(string->number (substring x 1))))
(string-split line ",")))
(define (find-distances ps)
(sort (map (lambda (p) (+ (abs (pos-x p))
(abs (pos-y p))))
(set->list ps))
<))
(define (part1 f)
(find-distances (set-intersect (calculate-segment-points (parse-line (read-line f)))
(calculate-segment-points (parse-line (read-line f))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment