Day 20: Refactoring. Will NOT be using sets.

This commit is contained in:
Jonathan Chan 2021-12-19 21:58:46 -08:00
parent e5c9bddeb3
commit b1ea233470
2 changed files with 16 additions and 29 deletions

View File

@ -1,10 +0,0 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

View File

@ -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 part2
(define (enhance* n)
(time
(for/fold ([img image]
[default #\.]
#:result (count #{char=? % #\#} (hash-values img)))
([_ (range 50)])
([_ (range n)])
(values (enhance default img)
(match default [#\. #\#] [#\# #\.]))))
(match default [#\. #\#] [#\# #\.])))))
(show-solution part1 part2)
(show-solution (enhance* 2) (enhance* 50))