Day 09: Use vectors instead of lists.
This commit is contained in:
parent
59b8087029
commit
efd3cc9059
32
src/09.rkt
32
src/09.rkt
|
@ -4,18 +4,18 @@
|
||||||
"../lib.rkt")
|
"../lib.rkt")
|
||||||
|
|
||||||
(define input
|
(define input
|
||||||
(~>> (problem-input 9)
|
(for/vector ([row (problem-input 9)])
|
||||||
(map string->list)
|
(for/vector ([col (string->list row)])
|
||||||
(map #{map char->number %})))
|
(char->number col))))
|
||||||
|
|
||||||
(define rows (length input))
|
(define rows (vector-length input))
|
||||||
(define cols (length (first input)))
|
(define cols (vector-length (vector-ref input 0)))
|
||||||
|
|
||||||
(define (grid-ref grid row col [default +inf.0])
|
(define (grid-ref grid row col [default +inf.0])
|
||||||
(if (or (< row 0) (>= row rows)
|
(if (or (< row 0) (>= row rows)
|
||||||
(< col 0) (>= col cols))
|
(< col 0) (>= col cols))
|
||||||
default
|
default
|
||||||
(list-ref (list-ref grid row) col)))
|
(vector-ref (vector-ref grid row) col)))
|
||||||
|
|
||||||
(define (neighbours row col)
|
(define (neighbours row col)
|
||||||
(list (cons (sub1 row) col)
|
(list (cons (sub1 row) col)
|
||||||
|
@ -44,16 +44,14 @@
|
||||||
(set-add seen loc))))))
|
(set-add seen loc))))))
|
||||||
|
|
||||||
(define-values (part1 part2)
|
(define-values (part1 part2)
|
||||||
(for/fold ([risk 0]
|
(for*/fold ([risk 0]
|
||||||
[basins '()]
|
[basins '()]
|
||||||
#:result (values risk (apply * (take (sort basins >) 3))))
|
#:result (values risk (apply * (take (sort basins >) 3))))
|
||||||
([row (range rows)])
|
([row (range rows)]
|
||||||
(for/fold ([risk risk]
|
[col (range cols)])
|
||||||
[basins basins])
|
(if (low? row col)
|
||||||
([col (range cols)])
|
(values (+ (add1 (grid-ref input row col)) risk)
|
||||||
(if (low? row col)
|
(cons (basin row col) basins))
|
||||||
(values (+ (add1 (grid-ref input row col)) risk)
|
(values risk basins))))
|
||||||
(cons (basin row col) basins))
|
|
||||||
(values risk basins)))))
|
|
||||||
|
|
||||||
(show-solution part1 part2)
|
(show-solution part1 part2)
|
Loading…
Reference in New Issue