1
0
Fork 0

Day 05: Now purely functional!

This commit is contained in:
Jonathan Chan 2021-12-04 22:43:10 -08:00 committed by Jonathan Chan
parent 611189c102
commit 040def513f
1 changed files with 27 additions and 22 deletions

View File

@ -15,30 +15,35 @@
(define (count-crosses grid)
(count #{> % 1} (hash-values grid)))
(define grid (make-hash))
(for ([hv hvs])
(define grid1
(for/fold ([grid (hash)])
([hv hvs])
(match hv
[`(,x ,y1 ,x ,y2)
(for ([y (range (min y1 y2) (add1 (max y1 y2)))])
(hash-update! grid `(,x ,y) add1 0))]
(for/fold ([grid grid])
([y (range (min y1 y2) (add1 (max y1 y2)))])
(hash-update grid `(,x ,y) add1 0))]
[`(,x1 ,y ,x2 ,y)
(for ([x (range (min x1 x2) (add1 (max x1 x2)))])
(hash-update! grid `(,x ,y) add1 0))]))
(for/fold ([grid grid])
([x (range (min x1 x2) (add1 (max x1 x2)))])
(hash-update grid `(,x ,y) add1 0))])))
(define part1 (count-crosses grid))
(define part1 (count-crosses grid1))
(for ([diag diags])
(define grid2
(for/fold ([grid grid1])
([diag diags])
(match diag
[`(,x1 ,y1 ,x2 ,y2)
(define-values (x-offset x-step)
(if (>= x2 x1) (values add1 1) (values sub1 -1)))
(define-values (y-offset y-step)
(if (>= y2 y1) (values add1 1) (values sub1 -1)))
(for ([x (range x1 (x-offset x2) x-step)]
(for/fold ([grid grid])
([x (range x1 (x-offset x2) x-step)]
[y (range y1 (y-offset y2) y-step)])
(hash-update! grid `(,x ,y) add1 0))]))
(hash-update grid `(,x ,y) add1 0))])))
(define part2 (count-crosses grid))
(define part2 (count-crosses grid2))
(show-solution part1 part2)