aboutsummaryrefslogtreecommitdiff
path: root/2021
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-13 08:16:41 -0500
committerHJ <thechairman@thechairman.info>2021-12-13 08:16:41 -0500
commit9314581a5576bd7cf8a73ea09b2fe6e159d67de1 (patch)
tree78ca7c7cc82d14d3a39f0607558f839d5317173f /2021
parentb101a7da2831464bf339f9a97cdea2efb1b51147 (diff)
downloadgleam_aoc-9314581a5576bd7cf8a73ea09b2fe6e159d67de1.tar.gz
gleam_aoc-9314581a5576bd7cf8a73ea09b2fe6e159d67de1.zip
day 13 done
Diffstat (limited to '2021')
-rw-r--r--2021/day-13/day-13.rkt64
1 files changed, 64 insertions, 0 deletions
diff --git a/2021/day-13/day-13.rkt b/2021/day-13/day-13.rkt
new file mode 100644
index 0000000..60cb95f
--- /dev/null
+++ b/2021/day-13/day-13.rkt
@@ -0,0 +1,64 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading)
+
+(define (make-pt x y)
+ (hash 'x x 'y y))
+(struct fold (dir loc) #:transparent)
+(define (make-fold dir loc)
+ (fold (string->symbol dir)
+ (string->number loc)))
+
+(define data (port->lines (open-day 13 2021)))
+(define-values (points-list folds-list) (splitf-at data (λ~> (equal? "") not)))
+
+(define pts
+ (for/set ([p (in-list points-list)])
+ (~> p
+ (string-split ",")
+ (map string->number _)
+ (apply make-pt _))))
+
+(define folds
+ (for/list ([f (in-list (rest folds-list))])
+ (~>> f
+ (regexp-match #px"fold along (.)=(.*)")
+ rest
+ (apply make-fold))))
+
+(define (fold-over f pts)
+ (define dir (fold-dir f))
+ (define loc (fold-loc f))
+ (for/set ([p (in-set pts)])
+ (cond
+ [(> (hash-ref p dir) loc) (hash-update p dir (λ (l) (- (* 2 loc) l)))]
+ [else p])))
+
+;; part 1
+(~>> pts
+ (fold-over (first folds))
+ set-count)
+
+;; part 2
+(define final-fold
+ (for/fold ([p pts])
+ ([f (in-list folds)])
+ (fold-over f p)))
+
+(for/list ([y (in-range 6)])
+ (~>> (for/list ([x (in-range 39)])
+ (if (set-member? final-fold (hash 'x x 'y y))
+ #\█
+ #\space))
+ (apply string)
+ println))
+
+#|
+for this data set, the result looks like
+" ██ █ █ ██ ██ ███ ██ ██ █ █"
+"█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █"
+"█ █ ████ █ █ █ █ █ █ █ █ █"
+"████ █ █ █ ██ █ ███ █ ██ ████ █ █"
+"█ █ █ █ █ █ █ █ █ █ █ █ █ █ █"
+"█ █ █ █ ███ ██ █ ███ █ █ ██ "
+|# \ No newline at end of file