aboutsummaryrefslogtreecommitdiff
path: root/2021
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-13 11:20:22 -0500
committerHJ <thechairman@thechairman.info>2021-12-13 11:20:22 -0500
commit73b164855c8a65090eff8be762080aad02b83b14 (patch)
tree384c0523c08411ad710ebbe24495cf89ce3593ea /2021
parent9314581a5576bd7cf8a73ea09b2fe6e159d67de1 (diff)
downloadgleam_aoc-73b164855c8a65090eff8be762080aad02b83b14.tar.gz
gleam_aoc-73b164855c8a65090eff8be762080aad02b83b14.zip
day 13 generalizing the solution
Diffstat (limited to '2021')
-rw-r--r--2021/day-13/day-13.rkt33
1 files changed, 19 insertions, 14 deletions
diff --git a/2021/day-13/day-13.rkt b/2021/day-13/day-13.rkt
index 60cb95f..73388c8 100644
--- a/2021/day-13/day-13.rkt
+++ b/2021/day-13/day-13.rkt
@@ -13,11 +13,11 @@
(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 _))))
+ (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))])
@@ -29,10 +29,10 @@
(define (fold-over f pts)
(define dir (fold-dir f))
(define loc (fold-loc f))
- (for/set ([p (in-set pts)])
+ (for/set ([pt (in-set pts)])
(cond
- [(> (hash-ref p dir) loc) (hash-update p dir (λ (l) (- (* 2 loc) l)))]
- [else p])))
+ [(> (hash-ref pt dir) loc) (hash-update pt dir (λ (l) (- (* 2 loc) l)))]
+ [else pt])))
;; part 1
(~>> pts
@@ -40,14 +40,19 @@
set-count)
;; part 2
-(define final-fold
- (for/fold ([p pts])
+(define final-pts
+ (for/fold ([pt pts])
([f (in-list folds)])
- (fold-over f p)))
+ (fold-over f pt)))
-(for/list ([y (in-range 6)])
- (~>> (for/list ([x (in-range 39)])
- (if (set-member? final-fold (hash 'x x 'y y))
+(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)