blob: 153eabc5ee1bda41d42370757efaa064d0bd26e7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#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 ([pt (in-list points-list)])
(~> pt (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 ([pt (in-set pts)])
(cond
[(> (hash-ref pt dir) loc) (hash-update pt dir (λ (l) (- (* 2 loc) l)))]
[else pt])))
;; part 1
(~>> pts (fold-over (first folds)) set-count)
;; part 2
(define final-pts
(for/fold ([pt pts]) ([f (in-list folds)])
(fold-over f pt)))
(define (max-dim pts dim)
(~>> (for/list ([pt (in-set pts)])
(hash-ref pt dim))
(apply max)))
(for ([y (in-inclusive-range 0 (max-dim final-pts 'y))])
(~>> (for/list ([x (in-inclusive-range 0 (max-dim final-pts 'x))])
(if (set-member? final-pts (hash 'x x 'y y)) #\█ #\space))
(apply string)
println))
#|
for this data set, the result looks like
" ██ █ █ ██ ██ ███ ██ ██ █ █"
"█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █"
"█ █ ████ █ █ █ █ █ █ █ █ █"
"████ █ █ █ ██ █ ███ █ ██ ████ █ █"
"█ █ █ █ █ █ █ █ █ █ █ █ █ █ █"
"█ █ █ █ ███ ██ █ ███ █ █ ██ "
|#
|