aboutsummaryrefslogtreecommitdiff
path: root/2015
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-11 21:50:59 -0500
committerHJ <thechairman@thechairman.info>2021-12-11 21:50:59 -0500
commit0acfa0750900ece33b1e5aaef492db2a33fa60d7 (patch)
tree22c16714fc8d93527ee9538d0ba273ccd6141aaa /2015
parent6f3bca8136bc6c25f3660c98c0dc6bec586627ab (diff)
downloadgleam_aoc-0acfa0750900ece33b1e5aaef492db2a33fa60d7.tar.gz
gleam_aoc-0acfa0750900ece33b1e5aaef492db2a33fa60d7.zip
2015 day 6
Diffstat (limited to '2015')
-rw-r--r--2015/day-06/day-06.rkt50
1 files changed, 50 insertions, 0 deletions
diff --git a/2015/day-06/day-06.rkt b/2015/day-06/day-06.rkt
new file mode 100644
index 0000000..2cafa4e
--- /dev/null
+++ b/2015/day-06/day-06.rkt
@@ -0,0 +1,50 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading)
+
+(struct instruction (todo x1 y1 x2 y2) #:transparent)
+
+(define (make-instruction lst)
+ (apply instruction (string->symbol (first lst)) (map string->number (rest lst))))
+
+(define instructions
+ (for/list ([l (in-lines (open-day 6 2015))])
+ (~>> l
+ (regexp-match #px"(turn on|toggle|turn off) (\\d{1,3}),(\\d{1,3}) through (\\d{1,3}),(\\d{1,3})")
+ rest
+ make-instruction)))
+
+(define (vector2d-modify! vec x y f)
+ (define pos (+ x (* 1000 y)))
+ (vector-set! vec pos (f (vector-ref vec pos))))
+
+;; part 1
+(define (todo inst)
+ (match (instruction-todo inst)
+ ['|turn on| (λ _ #true)]
+ ['|turn off| (λ _ #false)]
+ ['|toggle| not]))
+
+(define (modify-light-grid inst light-grid using)
+ (for ([x (inclusive-range (instruction-x1 inst) (instruction-x2 inst))])
+ (for ([y (inclusive-range (instruction-y1 inst) (instruction-y2 inst))])
+ (vector2d-modify! light-grid x y (using inst)))))
+
+(define light-grid
+ (make-vector (* 1000 1000) #false))
+(for ([i (in-list instructions)])
+ (modify-light-grid i light-grid todo))
+(vector-count identity light-grid)
+
+;; part 2
+(define (todo-dimmer inst)
+ (match (instruction-todo inst)
+ ['|turn on| add1]
+ ['|turn off| (λ (x) (max 0 (sub1 x)))]
+ ['|toggle| (curry + 2)]))
+
+(define dimmable-grid
+ (make-vector (* 1000 1000) 0))
+(for ([i (in-list instructions)])
+ (modify-light-grid i dimmable-grid todo-dimmer))
+(apply + (vector->list dimmable-grid))