Day 20: Refactoring. Will NOT be using sets.
This commit is contained in:
parent
499da3826a
commit
f55ee61584
|
@ -1,10 +0,0 @@
|
||||||
1163751742
|
|
||||||
1381373672
|
|
||||||
2136511328
|
|
||||||
3694931569
|
|
||||||
7463417111
|
|
||||||
1319128137
|
|
||||||
1359912421
|
|
||||||
3125421639
|
|
||||||
1293138521
|
|
||||||
2311944581
|
|
27
src/20.rkt
27
src/20.rkt
|
@ -22,11 +22,6 @@
|
||||||
[c (range* (sub1 col) (add1 col))])
|
[c (range* (sub1 col) (add1 col))])
|
||||||
(cons r c)))
|
(cons r c)))
|
||||||
|
|
||||||
(define (square->number pixels)
|
|
||||||
(~>> (map #{match % [#\. "0"] [#\# "1"]} pixels)
|
|
||||||
(apply string-append)
|
|
||||||
string->binary))
|
|
||||||
|
|
||||||
(define (image-bounds img)
|
(define (image-bounds img)
|
||||||
(define keys (hash-keys img))
|
(define keys (hash-keys img))
|
||||||
(define rows (map car keys))
|
(define rows (map car keys))
|
||||||
|
@ -34,6 +29,12 @@
|
||||||
(values (minimum rows) (maximum rows)
|
(values (minimum rows) (maximum rows)
|
||||||
(minimum cols) (maximum cols)))
|
(minimum cols) (maximum cols)))
|
||||||
|
|
||||||
|
(define (compute pixels)
|
||||||
|
(~>> (map #{match % [#\. "0"] [#\# "1"]} pixels)
|
||||||
|
(apply string-append)
|
||||||
|
string->binary
|
||||||
|
(vector-ref algorithm)))
|
||||||
|
|
||||||
(define (show img)
|
(define (show img)
|
||||||
(define-values (min-row max-row min-col max-col)
|
(define-values (min-row max-row min-col max-col)
|
||||||
(image-bounds img))
|
(image-bounds img))
|
||||||
|
@ -50,20 +51,16 @@
|
||||||
[col (range* (sub1 min-col) (add1 max-col))])
|
[col (range* (sub1 min-col) (add1 max-col))])
|
||||||
(~>> (square row col)
|
(~>> (square row col)
|
||||||
(map #{hash-ref img % default})
|
(map #{hash-ref img % default})
|
||||||
square->number
|
compute
|
||||||
(vector-ref algorithm)
|
|
||||||
(hash-set img* (cons row col)))))
|
(hash-set img* (cons row col)))))
|
||||||
|
|
||||||
(define part1
|
(define (enhance* n)
|
||||||
(let ([img (enhance #\# (enhance #\. image))])
|
(time
|
||||||
(count #{char=? % #\#} (hash-values img))))
|
|
||||||
|
|
||||||
(define part2
|
|
||||||
(for/fold ([img image]
|
(for/fold ([img image]
|
||||||
[default #\.]
|
[default #\.]
|
||||||
#:result (count #{char=? % #\#} (hash-values img)))
|
#:result (count #{char=? % #\#} (hash-values img)))
|
||||||
([_ (range 50)])
|
([_ (range n)])
|
||||||
(values (enhance default img)
|
(values (enhance default img)
|
||||||
(match default [#\. #\#] [#\# #\.]))))
|
(match default [#\. #\#] [#\# #\.])))))
|
||||||
|
|
||||||
(show-solution part1 part2)
|
(show-solution (enhance* 2) (enhance* 50))
|
Loading…
Reference in New Issue