Created
March 19, 2015 02:36
-
-
Save minikomi/e4f31c73dea1ae89bf0e to your computer and use it in GitHub Desktop.
Daily Programmer #206
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define width 91) | |
(define height 51) | |
(define radius 9) | |
(define test-input #<<GRID | |
......x...x....x............x............x.................................x............... | |
.........x...........x...................x.....x...........xx.............x................ | |
...........x.................x.x............x..........................x................x.. | |
......x...x.....................x.....x....x.........x......x.......x...x.................. | |
.x...x.....x................xx...........................x.....xx.....x............x....... | |
.....xx.......x..x........x.............xx........x.......x.....................x.......x.. | |
...x..x.x..x......x..............................................................x...x..... | |
........x..x......x......x...x.x....x.......x........x..x...........x.x...x..........xx.... | |
...............x.x....x...........x......x.............x..........................x........ | |
...................x........x.............................................................. | |
..x.x.....................................x..x.x......x......x............................. | |
......x.............................................................................x..x... | |
......x....x...............x............................................................... | |
............x.............x.............................x...............x................x. | |
..xx........xx............x...x......................x..................................... | |
........x........xx..............x.....................x.x.......x........................x | |
.......x....................xx............................................................. | |
............x...x.........x...xx...............x........................................... | |
.............................x...............xx..x...........x....x........x...x.......x.x. | |
..........x.......................x.....................................x.................. | |
...xx..x.x..................x........................x.....................x..x.......x.... | |
.............xx..........x...............x......................x.........x.........x....x. | |
...............................x.....................x.x................................... | |
...................x....x............................x...x.......x.............x....x.x.... | |
.x.xx........................x...................................x.....x.......xx.......... | |
.......x................................................................................... | |
.........x.....x.................x.................x...x.......x..................x........ | |
.......x................x.x...................................x...xx....x.....x...x........ | |
..............................................x..................x......................... | |
............................x........x.......x............................................x | |
..x.............x.....x...............x............x...x....x...x.......................... | |
.......................xx.................x...................x...................x.......x | |
.x.x.............x....x.................................x...........x..x..........x.....x.. | |
...x..x..x......................x...........x..........x.............xxx....x..........x... | |
...........................................................x............................... | |
x......x.....x................x...............x....................................x....... | |
..x...........................x............x..........x....x..............................x | |
.......................x.......xx...............x...x.x.................x..x............x.. | |
x................x.......x........x.............................x.x.x...................x.x | |
.......................x...x.......................................................x....... | |
.x..................x.....x..........................................x...........x......... | |
.x...................x........x.................x..........xx..................x..x........ | |
.x..........x...x...........................x.x....................x..x.......x............ | |
.............x...x..................x................x..x.x.....xxx..x...xx..x............. | |
.x...................x.x....x...x.................x.............................x.....x.... | |
......................x.x........x...........x...................................x......x.. | |
................x....................................x....x....x......x..............x..x.. | |
......x.........................................x..x......x.x.......x...................... | |
.x..............................x..........x.x....x.................x...................... | |
x..x...........x..x.x...x..........................................x..............xx....... | |
..xx......x.......x.x.................x......................................x............. | |
GRID | |
) | |
(define test-grid (map string->list (string-split test-input "\n"))) | |
(define (get-circle-points w h cx cy r) | |
(for*/list ([x (stream-map (λ (i) (+ cx i)) | |
(in-range (- r) (add1 r)))] | |
[y (stream-map (λ (i) (+ cy i)) | |
(in-range (- r) (add1 r)))] | |
#:unless (or | |
(> 0 x)(> 0 y) | |
(<= w x) (<= h y) | |
(< r (sqrt (+ (expt (- cx x) 2) | |
(expt (- cy y) 2)))))) | |
(list x y))) | |
(define (lookup grid point) | |
(match-define (list x y) point) | |
(list-ref (list-ref grid y) x)) | |
(define (count-wartered-crops grid w h cx cy r) | |
(define points (get-circle-points w h cx cy r)) | |
(for/fold ([n 0]) | |
([p points] | |
#:unless (eq? p (list cx cy))) | |
(if (char=? (lookup grid p) #\x) | |
(add1 n) | |
n))) | |
(define (find-max-point grid w h r) | |
(define (loop cx cy max max-point) | |
(if (and (= (- w 1) cx) | |
(= (- h 1) cy)) | |
(values max max-point) | |
(begin | |
(let* | |
([wartered-at-point | |
(count-wartered-crops grid w h cx cy r)] | |
[new-x (modulo (+ cx 1) w)] | |
[new-y (if (zero? new-x) (+ cy 1) cy)]) | |
(if (< max wartered-at-point) | |
(loop new-x new-y wartered-at-point (list cx cy)) | |
(loop new-x new-y max max-point)))))) | |
(loop 0 0 -1 #f)) | |
(define (display-wartered-grid grid w h r max-point) | |
(define wartered-points (get-circle-points w h (first max-point) (second max-point) r)) | |
(for* ([x (range h)] | |
[y (range w)]) | |
(let ([char-at-point (lookup grid (list y x))]) | |
(if (and | |
(member (list x y) wartered-points) | |
(char=? #\. char-at-point)) | |
(display "~") | |
(display char-at-point))) | |
(when (= y (- w 1)) (newline)))) | |
(define (dailyprogrammer-206) | |
(define-values [max max-point] | |
(find-max-point test-grid width height radius)) | |
(displayln (format "Max point is (~a, ~a) with ~a wartered." | |
(first max-point) | |
(second max-point) | |
max)) | |
(display-wartered-grid test-grid width height radius max-point)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: