aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2021/day-13/day-13.rkt64
-rw-r--r--jj-aoc.rkt2
2 files changed, 65 insertions, 1 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
diff --git a/jj-aoc.rkt b/jj-aoc.rkt
index 3b4bae5..62c1523 100644
--- a/jj-aoc.rkt
+++ b/jj-aoc.rkt
@@ -3,4 +3,4 @@
(provide open-day)
(define (open-day n [year 2021])
- (open-aoc-input (find-session) year n #:cache (string->path "../../cache"))) \ No newline at end of file
+ (open-aoc-input (find-session) year n #:cache (string->path "./cache"))) \ No newline at end of file