diff options
Diffstat (limited to '2021/day5')
-rw-r--r-- | 2021/day5/05a.rkt | 36 | ||||
-rw-r--r-- | 2021/day5/05a_grammar.rkt | 6 | ||||
-rw-r--r-- | 2021/day5/05b.rkt | 39 |
3 files changed, 81 insertions, 0 deletions
diff --git a/2021/day5/05a.rkt b/2021/day5/05a.rkt new file mode 100644 index 0000000..c24cb49 --- /dev/null +++ b/2021/day5/05a.rkt @@ -0,0 +1,36 @@ +#lang racket + +(require "./05a_grammar.rkt") + +(define ventCounts (make-hash)) + +(define (digit x) + x) + +(define (number . DIGITS) + (string->number (foldr string-append "" DIGITS))) + +(define (coord a _a b) + (list a b)) + +(define (line c1 _b _c _d _e c2) + (define steps (max (abs (- (car c2) (car c1))) + (abs (- (second c2) (second c1))))) + (define mx (/ (- (car c2) (car c1)) steps)) + (define my (/ (- (second c2) (second c1)) steps)) + (cond [(or (= 0 mx) (= 0 my)) (for ([s (+ 1 steps)]) + (define v (list (+ (car c1) (* mx s)) (+ (second c1) (* my s)))) + (hash-set! ventCounts v (+ 1 (hash-ref ventCounts v 0))))] + [else 0])) + + +(define (vents l _a . ls) + (cond [(null? ls) l] + [else (cons l (apply vents ls))])) + +(define-namespace-anchor anc) +(define ns (namespace-anchor->namespace anc)) +(define parsed (parse-to-datum (file->string "./input"))) +(void (eval parsed ns)) + +(length (filter (curry <= 2) (hash-values ventCounts))) diff --git a/2021/day5/05a_grammar.rkt b/2021/day5/05a_grammar.rkt new file mode 100644 index 0000000..cc78e6a --- /dev/null +++ b/2021/day5/05a_grammar.rkt @@ -0,0 +1,6 @@ +#lang brag +vents: (line "\n")* ["\n"] +line: coord " " "-" ">" " " coord +coord: number "," number +number: digit* +digit: "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" diff --git a/2021/day5/05b.rkt b/2021/day5/05b.rkt new file mode 100644 index 0000000..25d6fd6 --- /dev/null +++ b/2021/day5/05b.rkt @@ -0,0 +1,39 @@ +#lang racket + +(require "./05a_grammar.rkt") + +(define ventCounts (make-hash)) + +(define (digit x) + x) + +(define (number . DIGITS) + (string->number (foldr string-append "" DIGITS))) + +(define (coord a _a b) + (list a b)) + +(define (line c1 _b _c _d _e c2) + (define steps (max (abs (- (car c2) (car c1))) + (abs (- (second c2) (second c1))))) + (define mx (/ (- (car c2) (car c1)) steps)) + (define my (/ (- (second c2) (second c1)) steps)) + (for ([s (+ 1 steps)]) + (define v (list (+ (car c1) (* mx s)) (+ (second c1) (* my s)))) + (hash-set! ventCounts v (+ 1 (hash-ref ventCounts v 0))))) + +(define (vents l _a . ls) + (cond [(null? ls) l] + [else (cons l (apply vents ls))])) + +(define (print-board ex ey) + (for ([y ey]) + (for ([x ex]) + (display (hash-ref ventCounts (list x y) "."))) + (display "\n"))) + +(define-namespace-anchor anc) +(define ns (namespace-anchor->namespace anc)) +(define parsed (parse-to-datum (file->string "./input"))) +(void (eval parsed ns)) +(length (filter (curry <= 2) (hash-values ventCounts))) |