From f55ee6158457a6aa32843885e7081ce87aa76aac Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sun, 19 Dec 2021 21:58:46 -0800 Subject: [PATCH] Day 20: Refactoring. Will NOT be using sets. --- input/15-test.txt | 10 ---------- src/20.rkt | 35 ++++++++++++++++------------------- 2 files changed, 16 insertions(+), 29 deletions(-) delete mode 100644 input/15-test.txt diff --git a/input/15-test.txt b/input/15-test.txt deleted file mode 100644 index ab80887..0000000 --- a/input/15-test.txt +++ /dev/null @@ -1,10 +0,0 @@ -1163751742 -1381373672 -2136511328 -3694931569 -7463417111 -1319128137 -1359912421 -3125421639 -1293138521 -2311944581 diff --git a/src/20.rkt b/src/20.rkt index cf749ab..2a9fa6f 100644 --- a/src/20.rkt +++ b/src/20.rkt @@ -22,11 +22,6 @@ [c (range* (sub1 col) (add1 col))]) (cons r c))) -(define (square->number pixels) - (~>> (map #{match % [#\. "0"] [#\# "1"]} pixels) - (apply string-append) - string->binary)) - (define (image-bounds img) (define keys (hash-keys img)) (define rows (map car keys)) @@ -34,6 +29,12 @@ (values (minimum rows) (maximum rows) (minimum cols) (maximum cols))) +(define (compute pixels) + (~>> (map #{match % [#\. "0"] [#\# "1"]} pixels) + (apply string-append) + string->binary + (vector-ref algorithm))) + (define (show img) (define-values (min-row max-row min-col max-col) (image-bounds img)) @@ -50,20 +51,16 @@ [col (range* (sub1 min-col) (add1 max-col))]) (~>> (square row col) (map #{hash-ref img % default}) - square->number - (vector-ref algorithm) + compute (hash-set img* (cons row col))))) -(define part1 - (let ([img (enhance #\# (enhance #\. image))]) - (count #{char=? % #\#} (hash-values img)))) +(define (enhance* n) + (time + (for/fold ([img image] + [default #\.] + #:result (count #{char=? % #\#} (hash-values img))) + ([_ (range n)]) + (values (enhance default img) + (match default [#\. #\#] [#\# #\.]))))) -(define part2 - (for/fold ([img image] - [default #\.] - #:result (count #{char=? % #\#} (hash-values img))) - ([_ (range 50)]) - (values (enhance default img) - (match default [#\. #\#] [#\# #\.])))) - -(show-solution part1 part2) \ No newline at end of file +(show-solution (enhance* 2) (enhance* 50)) \ No newline at end of file