Day 20: Refactoring. Will NOT be using sets.
This commit is contained in:
parent
e5c9bddeb3
commit
b1ea233470
|
@ -1,10 +0,0 @@
|
|||
1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581
|
35
src/20.rkt
35
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)
|
||||
(show-solution (enhance* 2) (enhance* 50))
|
Loading…
Reference in New Issue