You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
38 lines
1.7 KiB
Racket
38 lines
1.7 KiB
Racket
3 years ago

#lang br



(require racket/file sugar rackunit racket/set)






(define lines (map (λ (s) (map (λ (s2) (define ints (map string>number (stringsplit s2 ",")))



(+ (first ints) (* +i (second ints)))) (stringsplit s " > "))) (file>lines "05.rktd")))






(define (Linenotdiagonal line)



(match line



[(list left right) (or (= (realpart left) (realpart right)) (= (imagpart left) (imagpart right)))]))






;; why does makepolar cause the solution to run faster?



;; both functions create an imaginary number



;; makepolar is slower to create the numbers (because it has to call trig functions)



;; but storing the polar numbers with frequencyhash is much faster



;; using inexact coefficients makes makerectangular go faster



;; but still not as fast as makepolar



(define gofast? #true)



(define imagfunc (if gofast? makepolar makerectangular))






(define (expand line)



(matchdefine (list x1 x2) (map realpart line))



(matchdefine (list y1 y2) (map imagpart line))



(cond



[(= x1 x2)



(for/list ([i (inrange (apply min (list y1 y2)) (add1 (apply max (list y1 y2))))])



(imagfunc x1 i))]



[(= y1 y2)



(for/list ([i (inrange (apply min (list x1 x2)) (add1 (apply max (list x1 x2))))])



(imagfunc i y1))]



[else (for/list ([x (inrange x1 ((if (> x1 x2) sub1 add1) x2) (if (> x1 x2) 1 1))]



[y (inrange y1 ((if (> y1 y2) sub1 add1) y2) (if (> y1 y2) 1 1))])



(imagfunc x y))]))






(define (calcresult points)



(length (filter (λ (x) (>= x 2)) (hashvalues (time (frequencyhash points))))))






(checkequal? (calcresult (appendmap expand (filter Linenotdiagonal lines))) 6113)



(checkequal? (calcresult (appendmap expand lines)) 20373)
