Day 20 minor refactoring.
This commit is contained in:
parent
83be0e883b
commit
178c6ab04f
67
src/20.rkt
67
src/20.rkt
|
@ -105,20 +105,36 @@
|
||||||
[image (map #{drop-right (drop % 1) 1} (drop-right (drop image 1) 1))])
|
[image (map #{drop-right (drop % 1) 1} (drop-right (drop image 1) 1))])
|
||||||
(map append rows image))))))
|
(map append rows image))))))
|
||||||
|
|
||||||
(define dimsize (length image))
|
;; Some vector grid helpers
|
||||||
(define image-vectors (lists->vectors image))
|
;; N.B. coord? = (row, column) = (list number? number?)
|
||||||
(define (vector-pos r c) (vector-ref (vector-ref image-vectors r) c))
|
|
||||||
|
;; lists->vectors: (listof (listof a)) -> (vectorof (vectorof a))
|
||||||
|
(define (lists->vectors list-grid)
|
||||||
|
(list->vector (map list->vector list-grid)))
|
||||||
|
|
||||||
|
;; vectors->lists: (vectorof (vectorof a)) -> (listof (listof a))
|
||||||
|
(define (vectors->lists vector-grid)
|
||||||
|
(vector->list (vector-map vector->list vector-grid)))
|
||||||
|
|
||||||
|
;; vector-grid-ref: (vectorof (vectorof a)) coord -> a
|
||||||
|
(define (vector-grid-ref vector-grid coord)
|
||||||
|
(vector-ref (vector-ref vector-grid (first coord)) (second coord)))
|
||||||
|
|
||||||
|
;; vector-grid-set!: (vectorof (vectorof a)) coord a -> void
|
||||||
|
(define (vector-grid-set! vector-grid coord v)
|
||||||
|
(vector-set! (vector-ref vector-grid (first coord)) (second coord) v))
|
||||||
|
|
||||||
#| (r, c) = O
|
#| (r, c) = O
|
||||||
#
|
#
|
||||||
# ## ## O## <--- friend
|
# ## ## O## <--- friend
|
||||||
# # # # # #
|
# # # # # #
|
||||||
|#
|
|#
|
||||||
(define (sea-monster-coords r c)
|
(define (sea-monster-coords coord)
|
||||||
(list `(,(- r 1) ,(+ c 1))
|
(match-let ([(list r c) coord])
|
||||||
`(,r ,c)
|
(list `(,r ,c)
|
||||||
`(,r ,(+ c 1))
|
`(,r ,(+ c 1))
|
||||||
`(,r ,(+ c 2))
|
`(,r ,(+ c 2))
|
||||||
|
`(,(- r 1) ,(+ c 1))
|
||||||
`(,r ,(- c 5))
|
`(,r ,(- c 5))
|
||||||
`(,r ,(- c 6))
|
`(,r ,(- c 6))
|
||||||
`(,r ,(- c 11))
|
`(,r ,(- c 11))
|
||||||
|
@ -129,37 +145,34 @@
|
||||||
`(,(+ r 1) ,(- c 7))
|
`(,(+ r 1) ,(- c 7))
|
||||||
`(,(+ r 1) ,(- c 10))
|
`(,(+ r 1) ,(- c 10))
|
||||||
`(,(+ r 1) ,(- c 13))
|
`(,(+ r 1) ,(- c 13))
|
||||||
`(,(+ r 1) ,(- c 16))))
|
`(,(+ r 1) ,(- c 16)))))
|
||||||
|
|
||||||
;; sea-monster?: number? number? -> boolean?
|
;; sea-monster?: coord? -> boolean?
|
||||||
(define (sea-monster? r c)
|
(define (sea-monster? image-vectors coord)
|
||||||
(define (pound? c) (char=? c #\#))
|
(andmap #{char=? (vector-grid-ref image-vectors %) #\#}
|
||||||
(andmap #{pound? (vector-pos (first %) (second %))}
|
(sea-monster-coords coord)))
|
||||||
(sea-monster-coords r c)))
|
|
||||||
|
|
||||||
;; A list of the neck coordinates of sea monsters
|
;; A list of the neck coordinates of sea monsters
|
||||||
;; sea-monsters: (listof (list number? number?))
|
;; sea-monsters: (listof (list number? number?))
|
||||||
(define sea-monsters
|
(define sea-monsters
|
||||||
|
(let ([image-vectors (lists->vectors image)]
|
||||||
|
[dimsize (length image)])
|
||||||
(for*/list ([r (range 1 (sub1 dimsize))]
|
(for*/list ([r (range 1 (sub1 dimsize))]
|
||||||
[c (range 17 (- dimsize 2))]
|
[c (range 17 (- dimsize 2))]
|
||||||
#:when (and (andmap #{char=? (vector-pos r %)}
|
#:when (sea-monster? image-vectors (list r c)))
|
||||||
(range c (+ c 3)))
|
(list r c))))
|
||||||
(sea-monster? r c)))
|
|
||||||
(list r c)))
|
|
||||||
|
|
||||||
;; Replace the # of a sea monster with O in image-vectors
|
;; Draw sea monsters in O and write to file
|
||||||
(define (reveal-sea-monster! r c)
|
|
||||||
(for-each #{vector-set! (vector-ref image-vectors (first %)) (second %) #\O}
|
|
||||||
(sea-monster-coords r c)))
|
|
||||||
|
|
||||||
;; Reveal the sea monsters in image-vectors, then write to file
|
|
||||||
(define (draw-sea-monsters!)
|
(define (draw-sea-monsters!)
|
||||||
(for-each #{reveal-sea-monster! (first %) (second %)} sea-monsters)
|
(let ([image-vectors (lists->vectors image)])
|
||||||
|
(for* ([sea-monster sea-monsters]
|
||||||
|
[coord (sea-monster-coords sea-monster)])
|
||||||
|
(vector-grid-set! image-vectors coord #\O))
|
||||||
(display-lines-to-file
|
(display-lines-to-file
|
||||||
(map (λ~> vector->list list->string)
|
(map list->string
|
||||||
(vector->list image-vectors))
|
(vectors->lists image-vectors))
|
||||||
"../input/20-image.txt"
|
"../input/20-image.txt"
|
||||||
#:exists 'replace))
|
#:exists 'replace)))
|
||||||
|
|
||||||
(define part1
|
(define part1
|
||||||
(* (tile-name (hash-ref image-hash (list 0 0)))
|
(* (tile-name (hash-ref image-hash (list 0 0)))
|
||||||
|
@ -169,6 +182,6 @@
|
||||||
|
|
||||||
(define part2
|
(define part2
|
||||||
(- (count #{char=? % #\#} (apply append image))
|
(- (count #{char=? % #\#} (apply append image))
|
||||||
(* (length sea-monsters) 15)))
|
(* (length sea-monsters) (length (sea-monster-coords (list 0 0))))))
|
||||||
|
|
||||||
(show-solution part1 part2)
|
(show-solution part1 part2)
|
||||||
|
|
Loading…
Reference in New Issue