Created
December 14, 2015 04:34
-
-
Save sbenhaim/f64394c74b829b736bb2 to your computer and use it in GitHub Desktop.
This file contains 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 | |
(require racket/match) | |
(define (get-coords sx sy ex ey) | |
(for*/list ([x (in-range sx (+ 1 ex))] | |
[y (in-range sy (+ 1 ey))]) | |
(list x y))) | |
(define (parse s) | |
(match-let ([(list _ cmd sx sy ex ey) | |
(regexp-match #rx"(turn on|turn off|toggle) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)" s)]) | |
(list cmd (string->number sx) (string->number sy) (string->number ex) (string->number ey)))) | |
(define (empty-board n) | |
(list->vector (for/list ([i n]) (make-vector n #f)))) | |
(define (update-board cmd board coords) | |
(for ([c coords]) | |
(let* ([x (car c)] | |
[y (cadr c)] | |
[r (vector-ref board y)] | |
[update (match cmd | |
["turn on" (lambda (_) #t)] | |
["turn off" (lambda (_) #f)] | |
["toggle" (lambda (v) (not v))])]) | |
(vector-set! r x (update (vector-ref r x))))) | |
board) | |
(let* ([in (open-input-file "a6.txt")] | |
[lines (in-lines in)] | |
[board (empty-board 1000)]) | |
(for ([l lines]) | |
(match (parse l) | |
[(list cmd sx sy ex ey) | |
(update-board cmd board (get-coords sx sy ex ey))])) | |
(length | |
(for*/list ([rs board] | |
[v rs] | |
#:when v) | |
v))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment