aboutsummaryrefslogtreecommitdiff
path: root/aoc2015/day-06
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
committerJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
commit8ab65dc2da1742eb86ec636c50c7018385b68167 (patch)
treec4fd556aca9b867cfa1f2f174128c30857353884 /aoc2015/day-06
parentfafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff)
downloadgleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz
gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2015/day-06')
-rw-r--r--aoc2015/day-06/day-06.rkt49
1 files changed, 49 insertions, 0 deletions
diff --git a/aoc2015/day-06/day-06.rkt b/aoc2015/day-06/day-06.rkt
new file mode 100644
index 0000000..d2eed08
--- /dev/null
+++ b/aoc2015/day-06/day-06.rkt
@@ -0,0 +1,49 @@
+#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))