aboutsummaryrefslogtreecommitdiff
path: root/2021
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-11-26 01:43:33 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-11-26 01:43:33 -0500
commitfeccf3f6f0a806b3317d1f399e3e8b42945c4f09 (patch)
treebf15ce045d1106c1b7f7de30c27540f40d0bf947 /2021
parent8b624fe7d2751337b1f16830cc9c041df73e99e7 (diff)
downloadgleam_aoc-feccf3f6f0a806b3317d1f399e3e8b42945c4f09.tar.gz
gleam_aoc-feccf3f6f0a806b3317d1f399e3e8b42945c4f09.zip
using raco fmt, replacing missing dependency
Diffstat (limited to '2021')
-rw-r--r--2021/day-02/day-02.rkt31
-rw-r--r--2021/day-03/day-03.rkt25
-rw-r--r--2021/day-04/day-04.rkt23
-rw-r--r--2021/day-05/day-05.rkt18
-rw-r--r--2021/day-06/day-06.rkt16
-rw-r--r--2021/day-07/day-07.rkt12
-rw-r--r--2021/day-08/day-08.rkt77
-rw-r--r--2021/day-09/day-09.rkt47
-rw-r--r--2021/day-10/day-10.rkt14
-rw-r--r--2021/day-11/day-11.rkt59
-rw-r--r--2021/day-12/day-12.rkt28
-rw-r--r--2021/day-13/day-13.rkt32
-rw-r--r--2021/day-14/day-14.rkt60
-rw-r--r--2021/day-15/day-15.rkt48
-rw-r--r--2021/day-16/day-16.rkt115
-rw-r--r--2021/day-17/day-17.rkt42
-rw-r--r--2021/day-19/day-19.rkt44
-rw-r--r--2021/day-20/day-20.rkt45
-rw-r--r--2021/day-21/day-21.rkt58
-rw-r--r--2021/day-25/day-25.rkt34
20 files changed, 292 insertions, 536 deletions
diff --git a/2021/day-02/day-02.rkt b/2021/day-02/day-02.rkt
index dbd1275..0bd0c3d 100644
--- a/2021/day-02/day-02.rkt
+++ b/2021/day-02/day-02.rkt
@@ -9,31 +9,16 @@
(chunks-of _ 2)))
;; part 1
-(for/fold ([depth 0]
- [position 0]
- #:result (* depth position))
- ([motion (in-list motion-data)])
+(for/fold ([depth 0] [position 0] #:result (* depth position)) ([motion (in-list motion-data)])
(match motion
- [(list 'forward x) (values depth
- (+ position x))]
- [(list 'up x) (values (- depth x)
- position)]
- [(list 'down x) (values (+ depth x)
- position)]))
+ [(list 'forward x) (values depth (+ position x))]
+ [(list 'up x) (values (- depth x) position)]
+ [(list 'down x) (values (+ depth x) position)]))
;; part 2
-(for/fold ([aim 0]
- [depth 0]
- [position 0]
- #:result (* depth position))
+(for/fold ([aim 0] [depth 0] [position 0] #:result (* depth position))
([motion (in-list motion-data)])
(match motion
- [(list 'forward x) (values aim
- (+ depth (* aim x))
- (+ position x))]
- [(list 'up x) (values (- aim x)
- depth
- position)]
- [(list 'down x) (values (+ aim x)
- depth
- position)])) \ No newline at end of file
+ [(list 'forward x) (values aim (+ depth (* aim x)) (+ position x))]
+ [(list 'up x) (values (- aim x) depth position)]
+ [(list 'down x) (values (+ aim x) depth position)]))
diff --git a/2021/day-03/day-03.rkt b/2021/day-03/day-03.rkt
index f26d5a4..95b7efd 100644
--- a/2021/day-03/day-03.rkt
+++ b/2021/day-03/day-03.rkt
@@ -9,31 +9,20 @@
;; part 1
(define most-common-bits
- (for*/list ([row (in-list (apply map list data))]
- [len (in-value (length data))])
- (if (> (count (λ (c) (char=? #\1 c)) row) (/ len 2))
- #\1
- #\0)))
+ (for*/list ([row (in-list (apply map list data))] [len (in-value (length data))])
+ (if (> (count (λ (c) (char=? #\1 c)) row) (/ len 2)) #\1 #\0)))
(define (bit-list->number lst)
- (~> lst
- (apply string _)
- (string->number _ 2)))
+ (~> lst (apply string _) (string->number _ 2)))
-(define gamma
- (bit-list->number most-common-bits))
-(define epsilon
- (~> most-common-bits
- (map (λ (c) (if (char=? c #\1) #\0 #\1)) _)
- bit-list->number))
+(define gamma (bit-list->number most-common-bits))
+(define epsilon (~> most-common-bits (map (λ (c) (if (char=? c #\1) #\0 #\1)) _) bit-list->number))
(* gamma epsilon)
;; part 2
(define (rating-search data comparison)
- (for/fold ([candidates data]
- #:result (bit-list->number (first candidates)))
- ([bit (in-list most-common-bits)]
- [bit-index (in-range 0 (length most-common-bits))])
+ (for/fold ([candidates data] #:result (bit-list->number (first candidates)))
+ ([bit (in-list most-common-bits)] [bit-index (in-range 0 (length most-common-bits))])
#:break (= 1 (length candidates))
(define keep-bit
(~> candidates
diff --git a/2021/day-04/day-04.rkt b/2021/day-04/day-04.rkt
index fdf1ea1..c572f74 100644
--- a/2021/day-04/day-04.rkt
+++ b/2021/day-04/day-04.rkt
@@ -1,7 +1,6 @@
#lang racket
(require advent-of-code
threading
- (only-in awesome-list transpose)
(only-in algorithms chunks-of))
(define data
@@ -9,17 +8,9 @@
#:unless (equal? l ""))
l))
-(define call-sheet
- (~> data
- car
- (string-split ",")
- (map string->number _)))
+(define call-sheet (~> data car (string-split ",") (map string->number _)))
(define bingo-cards
- (~> data
- cdr
- (map string-split _)
- (map (λ (row) (map string->number row)) _)
- (chunks-of 5)))
+ (~> data cdr (map string-split _) (map (λ (row) (map string->number row)) _) (chunks-of 5)))
(define test-card (first bingo-cards))
@@ -29,7 +20,7 @@
(if (eq? col call) 'X col))))
(define (check-card card)
- (for/or ([row (in-sequences card (transpose card))])
+ (for/or ([row (in-sequences card (apply map list card))])
(equal? row '(X X X X X))))
(define (multiply-by-last-call n call)
@@ -47,8 +38,7 @@
(apply + _)))
([call (in-list calls)])
#:break (check current-cards)
- (values (for/list ([card (in-list current-cards)]
- #:unless (exception card))
+ (values (for/list ([card (in-list current-cards)] #:unless (exception card))
(mark-card card call))
call)))
@@ -57,6 +47,5 @@
;; part 2
(evaluate-cards bingo-cards
call-sheet
- (λ (cards) (and (= (length cards) 1)
- (check-card (first cards))))
- check-card) \ No newline at end of file
+ (λ (cards) (and (= (length cards) 1) (check-card (first cards))))
+ check-card)
diff --git a/2021/day-05/day-05.rkt b/2021/day-05/day-05.rkt
index 49ce502..e568490 100644
--- a/2021/day-05/day-05.rkt
+++ b/2021/day-05/day-05.rkt
@@ -4,25 +4,20 @@
(define data
(for/list ([l (in-lines (open-aoc-input (find-session) 2021 5 #:cache (string->path "./cache")))])
- (~> l
- (string-replace " -> " ",")
- (string-split ",")
- (map string->number _))))
+ (~> l (string-replace " -> " ",") (string-split ",") (map string->number _))))
(define (trace-line! x y vec)
(define linear-coord (+ y (* 1000 x)))
- (vector-set! vec
- linear-coord
- (+ 1 (vector-ref vec linear-coord))))
+ (vector-set! vec linear-coord (+ 1 (vector-ref vec linear-coord))))
(define/match (orthogonal? coord-pair)
[((or (list n _ n _) (list _ n _ n))) #t]
[(_) #f])
-(define-values (orthogonal-lines diagonal-lines)
- (partition orthogonal? data))
+(define-values (orthogonal-lines diagonal-lines) (partition orthogonal? data))
-(define (dir a b) (if (< a b) 1 -1))
+(define (dir a b)
+ (if (< a b) 1 -1))
(define (trace-orthogonal! coord-pairs tracer result)
(for ([coord-pair (in-list coord-pairs)])
@@ -37,8 +32,7 @@
(define (trace-diagonal! coord-pairs tracer result)
(for ([coord-pair (in-list coord-pairs)])
(match-define (list x1 y1 x2 y2) coord-pair)
- (for ([x (inclusive-range x1 x2 (dir x1 x2))]
- [y (inclusive-range y1 y2 (dir y1 y2))])
+ (for ([x (inclusive-range x1 x2 (dir x1 x2))] [y (inclusive-range y1 y2 (dir y1 y2))])
(tracer x y result))))
;; part 1
diff --git a/2021/day-06/day-06.rkt b/2021/day-06/day-06.rkt
index f687db9..d8855ba 100644
--- a/2021/day-06/day-06.rkt
+++ b/2021/day-06/day-06.rkt
@@ -1,6 +1,6 @@
#lang racket
(require advent-of-code
- awesome-list
+ list-utils
threading
racket/hash)
@@ -12,22 +12,16 @@
(map string->number _)))
(define (simulate-fish time-period)
- (for/fold ([state (frequencies fish-data)]
- #:result (~> state
- hash-values
- (apply + _)))
+ (for/fold ([state (frequencies fish-data)] #:result (~> state hash-values (apply + _)))
([day (inclusive-range 1 time-period)])
(define day-older-fish
(for/hash ([(days pop) (in-hash state)])
(values (sub1 days) pop)))
- (define breeding-fish
- (hash-ref day-older-fish -1 0))
- (hash-union (hash-remove day-older-fish -1)
- (hash 8 breeding-fish 6 breeding-fish)
- #:combine +)))
+ (define breeding-fish (hash-ref day-older-fish -1 0))
+ (hash-union (hash-remove day-older-fish -1) (hash 8 breeding-fish 6 breeding-fish) #:combine +)))
;; part 1
(simulate-fish 80)
;; part 2
-(simulate-fish 256) \ No newline at end of file
+(simulate-fish 256)
diff --git a/2021/day-07/day-07.rkt b/2021/day-07/day-07.rkt
index 81a2e4f..89d5009 100644
--- a/2021/day-07/day-07.rkt
+++ b/2021/day-07/day-07.rkt
@@ -10,21 +10,19 @@
(string-split ",")
(map string->number _)))
-(define (gauss-sum n) (/ (* n (+ n 1)) 2))
+(define (gauss-sum n)
+ (/ (* n (+ n 1)) 2))
(define (compute-fuel-use f crabs align-to)
- (for/sum ([crab (in-list crabs)])
- (f (abs (- crab align-to)))))
+ (for/sum ([crab (in-list crabs)]) (f (abs (- crab align-to)))))
;; using the fact that the optimum location is at the median
;; of the crabs' starting location for the linear relationship
;; and at a coordinate within 1 unit of the mean for the quadratic one
-(~>> crab-data
- (median <)
- (compute-fuel-use identity crab-data))
+(~>> crab-data (median <) (compute-fuel-use identity crab-data))
(~>> crab-data
mean
((λ (m) (list (floor m) (ceiling m))))
(map (curry compute-fuel-use gauss-sum crab-data))
- (apply min)) \ No newline at end of file
+ (apply min))
diff --git a/2021/day-08/day-08.rkt b/2021/day-08/day-08.rkt
index ff54817..6476eae 100644
--- a/2021/day-08/day-08.rkt
+++ b/2021/day-08/day-08.rkt
@@ -1,67 +1,48 @@
#lang racket
(require threading
- awesome-list
+ list-utils
"../../jj-aoc.rkt")
(struct trial-data (signal output) #:transparent)
(define (string->sets s)
- (~> s
- string-split
- (map (λ~> string->list
- list->set) _)))
+ (~> s string-split (map (λ~> string->list list->set) _)))
(define data
- (for/list ([l (in-lines (open-day 8))]
- #:unless (equal? l ""))
- (~> l
- (string-split _ " | ")
- (map string->sets _)
- (apply trial-data _))))
+ (for/list ([l (in-lines (open-day 8))] #:unless (equal? l ""))
+ (~> l (string-split _ " | ") (map string->sets _) (apply trial-data _))))
;; part 1
-(for*/sum ([trial (in-list data)]
- [output (in-list (trial-data-output trial))]
- #:when (ormap (λ~> (= (set-count output))) '(2 3 4 7)))
- 1)
+(for*/sum ([trial (in-list data)] [output (in-list (trial-data-output trial))]
+ #:when (ormap (λ~> (= (set-count output))) '(2 3 4 7)))
+ 1)
;; part 2
(define (matching-pattern len trial)
(define solution-set
- (for*/list ([signal (in-list (trial-data-signal trial))]
- #:when (= (set-count signal) len))
+ (for*/list ([signal (in-list (trial-data-signal trial))] #:when (= (set-count signal) len))
signal))
(match solution-set
[(list s) s]
[s (apply set-intersect s)]))
(define (determine-arrangement t)
- (let*
- ([pattern-1 (matching-pattern 2 t)]
- [pattern-4 (matching-pattern 4 t)]
- [pattern-7 (matching-pattern 3 t)]
- [pattern-8 (matching-pattern 7 t)]
- [pattern-shared-235 (matching-pattern 5 t)]
- [pattern-3 (set-union pattern-1
- pattern-shared-235)]
- [pattern-9 (set-union pattern-4
- pattern-shared-235)]
- [pattern-shared-069 (matching-pattern 6 t)]
- [pattern-just-f (set-subtract pattern-shared-069 pattern-shared-235)]
- [pattern-just-e (set-subtract pattern-8
- (set-union pattern-4
- pattern-shared-235
- pattern-shared-069))]
- [pattern-2 (set-union (set-subtract pattern-3 pattern-just-f)
- pattern-just-e)]
- [pattern-just-c (set-subtract (set-intersect pattern-4 pattern-7)
- pattern-just-f)]
- [pattern-6 (set-subtract pattern-8 pattern-just-c)]
- [pattern-5 (set-subtract pattern-6 pattern-just-e)]
- [pattern-0 (set-union (set-subtract pattern-8
- pattern-shared-235)
- pattern-shared-069)]
- )
+ (let* ([pattern-1 (matching-pattern 2 t)]
+ [pattern-4 (matching-pattern 4 t)]
+ [pattern-7 (matching-pattern 3 t)]
+ [pattern-8 (matching-pattern 7 t)]
+ [pattern-shared-235 (matching-pattern 5 t)]
+ [pattern-3 (set-union pattern-1 pattern-shared-235)]
+ [pattern-9 (set-union pattern-4 pattern-shared-235)]
+ [pattern-shared-069 (matching-pattern 6 t)]
+ [pattern-just-f (set-subtract pattern-shared-069 pattern-shared-235)]
+ [pattern-just-e
+ (set-subtract pattern-8 (set-union pattern-4 pattern-shared-235 pattern-shared-069))]
+ [pattern-2 (set-union (set-subtract pattern-3 pattern-just-f) pattern-just-e)]
+ [pattern-just-c (set-subtract (set-intersect pattern-4 pattern-7) pattern-just-f)]
+ [pattern-6 (set-subtract pattern-8 pattern-just-c)]
+ [pattern-5 (set-subtract pattern-6 pattern-just-e)]
+ [pattern-0 (set-union (set-subtract pattern-8 pattern-shared-235) pattern-shared-069)])
(~> (list pattern-0
pattern-1
pattern-2
@@ -76,8 +57,8 @@
make-hash)))
(for/sum ([trial (in-list data)])
- (~>> trial
- trial-data-output
- (map (λ~>> (hash-ref (determine-arrangement trial))))
- (apply ~a)
- string->number))
+ (~>> trial
+ trial-data-output
+ (map (λ~>> (hash-ref (determine-arrangement trial))))
+ (apply ~a)
+ string->number))
diff --git a/2021/day-09/day-09.rkt b/2021/day-09/day-09.rkt
index 7c55d43..d550a9e 100644
--- a/2021/day-09/day-09.rkt
+++ b/2021/day-09/day-09.rkt
@@ -4,12 +4,8 @@
"../../jj-aoc.rkt")
(define sea-floor-data
- (for/vector ([l (in-lines (open-day 9))]
- #:unless (equal? l ""))
- (~>> l
- string->list
- (map (λ~>> ~a string->number))
- list->vector)))
+ (for/vector ([l (in-lines (open-day 9))] #:unless (equal? l ""))
+ (~>> l string->list (map (λ~>> ~a string->number)) list->vector)))
(define max-rows (vector-length sea-floor-data))
(define max-cols (vector-length (vector-ref sea-floor-data 0)))
@@ -17,35 +13,25 @@
(define (vector2d-ref vec coord)
(match-define `(,r ,c) coord)
- (~> vec
- (vector-ref r)
- (vector-ref c)))
+ (~> vec (vector-ref r) (vector-ref c)))
(define (adjacent coords)
(match-define `(,r ,c) coords)
- `((,(add1 r) ,c)
- (,(sub1 r) ,c)
- (,r ,(add1 c))
- (,r ,(sub1 c))))
+ `((,(add1 r) ,c) (,(sub1 r) ,c) (,r ,(add1 c)) (,r ,(sub1 c))))
(define (valid-coordinate coord)
(match-define `(,r ,c) coord)
- (and (>= r min-rows)
- (< r max-rows)
- (>= c min-cols)
- (< c max-cols)))
+ (and (>= r min-rows) (< r max-rows) (>= c min-cols) (< c max-cols)))
;; part 1
(define (lowest-point? vec coord)
- (for*/and ([neighbor (in-list (adjacent coord))]
- #:when (valid-coordinate neighbor))
+ (for*/and ([neighbor (in-list (adjacent coord))] #:when (valid-coordinate neighbor))
(< (vector2d-ref vec coord) (vector2d-ref vec neighbor))))
-(for*/sum ([r (in-range min-rows max-rows)]
- [c (in-range min-cols max-cols)]
- [coord (in-value `(,r ,c))]
- #:when (lowest-point? sea-floor-data coord))
- (add1 (vector2d-ref sea-floor-data coord)))
+(for*/sum ([r (in-range min-rows max-rows)] [c (in-range min-cols max-cols)]
+ [coord (in-value `(,r ,c))]
+ #:when (lowest-point? sea-floor-data coord))
+ (add1 (vector2d-ref sea-floor-data coord)))
;; part 2
;; all the basins are bordered by the edges or by ridges of elevation 9,
@@ -53,16 +39,14 @@
(define walked (make-hash))
(define (walkable? vec coord)
- (and (< (vector2d-ref vec coord) 9)
- (not (hash-has-key? walked coord))))
+ (and (< (vector2d-ref vec coord) 9) (not (hash-has-key? walked coord))))
(define (walk-the-basin vec coord)
(cond
[(walkable? vec coord)
(hash-set! walked coord 'visited)
- (add1 (for/sum [(neighbor (in-list (adjacent coord)))
- #:when (valid-coordinate neighbor)]
- (walk-the-basin vec neighbor)))]
+ (add1 (for/sum [(neighbor (in-list (adjacent coord))) #:when (valid-coordinate neighbor)]
+ (walk-the-basin vec neighbor)))]
[else 0]))
(define basins
@@ -72,7 +56,4 @@
#:when (walkable? sea-floor-data coord))
(walk-the-basin sea-floor-data coord)))
-(~> basins
- (sort >)
- (take 3)
- (apply * _)) \ No newline at end of file
+(~> basins (sort >) (take 3) (apply * _))
diff --git a/2021/day-10/day-10.rkt b/2021/day-10/day-10.rkt
index d40d1ea..ea1b389 100644
--- a/2021/day-10/day-10.rkt
+++ b/2021/day-10/day-10.rkt
@@ -4,8 +4,7 @@
threading
"../../jj-aoc.rkt")
-(define chunks
- (port->lines (open-day 10 2021)))
+(define chunks (port->lines (open-day 10 2021)))
(define (opening-bracket? c)
(member c (string->list "([{<")))
@@ -16,10 +15,8 @@
(define (parse-brackets lst [acc '()])
(cond
[(empty? lst) acc]
- [(opening-bracket? (first lst))
- (parse-brackets (rest lst) (cons (first lst) acc))]
- [(matching-brackets? (first acc) (first lst))
- (parse-brackets (rest lst) (rest acc))]
+ [(opening-bracket? (first lst)) (parse-brackets (rest lst) (cons (first lst) acc))]
+ [(matching-brackets? (first acc) (first lst)) (parse-brackets (rest lst) (rest acc))]
[else (get-score (first lst))]))
;; part 1
@@ -39,8 +36,7 @@
;; part 2
(define (completion-score lst)
- (for/fold ([score 0])
- ([c (in-list lst)])
+ (for/fold ([score 0]) ([c (in-list lst)])
(define val
(match (string c)
["(" 1]
@@ -58,4 +54,4 @@
[score (in-value (score-incomplete-string chunk))]
#:when (> score 0))
score)
- (median < )) \ No newline at end of file
+ (median <))
diff --git a/2021/day-11/day-11.rkt b/2021/day-11/day-11.rkt
index cb784f0..bc22991 100644
--- a/2021/day-11/day-11.rkt
+++ b/2021/day-11/day-11.rkt
@@ -9,11 +9,8 @@
(apply append)))
(define octopus-data
- (~>> (for/list ([l (in-lines (open-day 11 2021))]
- #:unless (equal? l ""))
- (~>> l
- string->list
- (map (λ~>> ~a string->number))))
+ (~>> (for/list ([l (in-lines (open-day 11 2021))] #:unless (equal? l ""))
+ (~>> l string->list (map (λ~>> ~a string->number))))
(apply append)
(map cons coords)
make-hash))
@@ -23,24 +20,18 @@
(define (adjacent-to coord)
(match-define (cons r c) coord)
- (for*/list ([row (in-list '(-1 0 1))]
- [col (in-list '(-1 0 1))]
- #:unless (= 0 row col))
+ (for*/list ([row (in-list '(-1 0 1))] [col (in-list '(-1 0 1))] #:unless (= 0 row col))
(cons (+ r row) (+ c col))))
(define (simulate-octopi-step data)
(define flashed-this-step (mutable-set))
-
- (let look-for-more-flashes
- ([octopi (for/hash ([(k v) data])
- (values k (add1 v)))]
- [flashes-so-far 0])
- (define-values (next-octopus-update
- flashes-this-update)
+
+ (let look-for-more-flashes ([octopi (for/hash ([(k v) data])
+ (values k (add1 v)))]
+ [flashes-so-far 0])
+ (define-values (next-octopus-update flashes-this-update)
(for*/fold ([octopi octopi] [flashes 0])
- ([(p x) (in-hash octopi)]
- #:when (> x 9)
- #:unless (set-member? flashed-this-step p))
+ ([(p x) (in-hash octopi)] #:when (> x 9) #:unless (set-member? flashed-this-step p))
(set-add! flashed-this-step p)
(define flashed-octopi
(for*/fold ([o (hash-set octopi p 0)])
@@ -48,32 +39,18 @@
#:when (hash-has-key? o adj)
#:unless (set-member? flashed-this-step adj))
(hash-update o adj add1)))
- (values flashed-octopi
- (add1 flashes))))
+ (values flashed-octopi (add1 flashes))))
(if (> flashes-this-update 0)
- (look-for-more-flashes next-octopus-update
- (+ flashes-so-far
- flashes-this-update))
- (values next-octopus-update
- flashes-so-far))))
+ (look-for-more-flashes next-octopus-update (+ flashes-so-far flashes-this-update))
+ (values next-octopus-update flashes-so-far))))
;; part 1
-(for/fold ([octopi octopus-data]
- [total-flashes 0] #:result total-flashes)
- ([step (in-range 100)])
- (define-values [next-state
- flashes-from-this-state]
- (simulate-octopi-step octopi))
- (values next-state
- (+ total-flashes flashes-from-this-state)))
+(for/fold ([octopi octopus-data] [total-flashes 0] #:result total-flashes) ([step (in-range 100)])
+ (define-values [next-state flashes-from-this-state] (simulate-octopi-step octopi))
+ (values next-state (+ total-flashes flashes-from-this-state)))
;; part 2
-(for/fold ([octopi octopus-data]
- [synchro-step 0] #:result synchro-step)
- ([step (in-naturals 1)])
- (define-values [next-state
- flashes-from-this-state]
- (simulate-octopi-step octopi))
+(for/fold ([octopi octopus-data] [synchro-step 0] #:result synchro-step) ([step (in-naturals 1)])
+ (define-values [next-state flashes-from-this-state] (simulate-octopi-step octopi))
#:final (= flashes-from-this-state 100)
- (values next-state
- step))
+ (values next-state step))
diff --git a/2021/day-12/day-12.rkt b/2021/day-12/day-12.rkt
index 76a5aa8..18ed86f 100644
--- a/2021/day-12/day-12.rkt
+++ b/2021/day-12/day-12.rkt
@@ -11,23 +11,14 @@
(for ([pair (in-list path-pairs)])
(match-define (cons start end) pair)
- (hash-update! edges-hash
- start
- (curry cons end)
- '())
- (hash-update! edges-hash
- end
- (curry cons start)
- '()))
+ (hash-update! edges-hash start (curry cons end) '())
+ (hash-update! edges-hash end (curry cons start) '()))
;; part 1
(define (backtracking-disallowed? next prevs)
- (and (equal? (string-downcase next) next)
- (member next prevs)))
+ (and (equal? (string-downcase next) next) (member next prevs)))
-(define (look-for-next-cave
- [path-list '("start")]
- #:only-one-visit? [visit-used-up? #t])
+(define (look-for-next-cave [path-list '("start")] #:only-one-visit? [visit-used-up? #t])
(define current-cave (car path-list))
(cond
[(equal? current-cave "end") (list path-list)]
@@ -38,15 +29,10 @@
visit-used-up?))))
(look-for-next-cave
(cons next-path path-list)
- #:only-one-visit? (or (backtracking-disallowed? next-path path-list)
- visit-used-up?)))
+ #:only-one-visit? (or (backtracking-disallowed? next-path path-list) visit-used-up?)))
(apply append))]))
-(~> (look-for-next-cave)
- length
- time)
+(~> (look-for-next-cave) length time)
;; part 2
-(~> (look-for-next-cave #:only-one-visit? #f)
- length
- time) \ No newline at end of file
+(~> (look-for-next-cave #:only-one-visit? #f) length time)
diff --git a/2021/day-13/day-13.rkt b/2021/day-13/day-13.rkt
index 73388c8..153eabc 100644
--- a/2021/day-13/day-13.rkt
+++ b/2021/day-13/day-13.rkt
@@ -6,43 +6,33 @@
(hash 'x x 'y y))
(struct fold (dir loc) #:transparent)
(define (make-fold dir loc)
- (fold (string->symbol dir)
- (string->number 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 _))))
+ (~> 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))))
+ (~>> 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])))
+ (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)
+(~>> pts (fold-over (first folds)) set-count)
;; part 2
(define final-pts
- (for/fold ([pt pts])
- ([f (in-list folds)])
+ (for/fold ([pt pts]) ([f (in-list folds)])
(fold-over f pt)))
(define (max-dim pts dim)
@@ -52,9 +42,7 @@
(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))
+ (if (set-member? final-pts (hash 'x x 'y y)) #\█ #\space))
(apply string)
println))
@@ -66,4 +54,4 @@ for this data set, the result looks like
"████ █ █ █ ██ █ ███ █ ██ ████ █ █"
"█ █ █ █ █ █ █ █ █ █ █ █ █ █ █"
"█ █ █ █ ███ ██ █ ███ █ █ ██ "
-|# \ No newline at end of file
+|#
diff --git a/2021/day-14/day-14.rkt b/2021/day-14/day-14.rkt
index d37786e..e445694 100644
--- a/2021/day-14/day-14.rkt
+++ b/2021/day-14/day-14.rkt
@@ -3,67 +3,59 @@
threading
memoize
algorithms
- awesome-list)
+ list-utils)
(define data (port->lines (open-day 14 2021)))
(define (starting-polymer d)
- (~> d
- first
- string->list
- (sliding 2 1)))
+ (~> d first string->list (sliding 2 1)))
(define (first-char d)
(first (first (starting-polymer d))))
(define (starting-counts d)
- (~> d
- first
- frequencies
- hash->list
- make-hash))
+ (~> d first frequencies hash->list make-hash))
(define (starting-pairs d)
- (~>> d
- (drop _ 2)
- (map (λ~> (substring 0 2) string->list))))
+ (~>> d (drop _ 2) (map (λ~> (substring 0 2) string->list))))
(define (new-pairs d)
(~>> d
(drop _ 2)
(map (λ~> (string-replace " -> " "")
- string->list
- ((match-lambda [(list a b c) (list a c b)]) _)
- (sliding 2 1)))))
+ string->list
+ ((match-lambda
+ [(list a b c) (list a c b)])
+ _)
+ (sliding 2 1)))))
(define (transform d)
- (~>> (map list (starting-pairs d) (new-pairs d))
- (append*)
- (apply hash)))
+ (~>> (map list (starting-pairs d) (new-pairs d)) (append*) (apply hash)))
(define transformation (transform data))
(define/memo (get-count polymer times)
- (match times
- [0 (for/fold ([counts (hash)])
- ([pair (in-list polymer)])
- (hash-update counts (second pair) add1 0))]
- [_ (for*/fold ([counts (hash)])
- ([pair (in-list polymer)]
- [(c n) (in-hash (get-count (hash-ref transformation pair) (sub1 times)))])
- (hash-update counts c (λ~> (+ n)) 0))]))
+ (match times
+ [0
+ (for/fold ([counts (hash)]) ([pair (in-list polymer)])
+ (hash-update counts (second pair) add1 0))]
+ [_
+ (for*/fold ([counts (hash)])
+ ([pair (in-list polymer)]
+ [(c n) (in-hash (get-count (hash-ref transformation pair) (sub1 times)))])
+ (hash-update counts c (λ~> (+ n)) 0))]))
;; part 1
(define (process-polymer d n)
(~> d
- starting-polymer
- (get-count _ n)
- (hash-update _ (first-char d) add1 0)
- hash-values
- (sort >)
- ((λ (l) (- (first l) (last l))))))
+ starting-polymer
+ (get-count _ n)
+ (hash-update _ (first-char d) add1 0)
+ hash-values
+ (sort >)
+ ((λ (l) (- (first l) (last l))))))
(process-polymer data 10)
;; part 2
-(process-polymer data 40) \ No newline at end of file
+(process-polymer data 40)
diff --git a/2021/day-15/day-15.rkt b/2021/day-15/day-15.rkt
index e9101a6..5e61c55 100644
--- a/2021/day-15/day-15.rkt
+++ b/2021/day-15/day-15.rkt
@@ -6,33 +6,23 @@
(struct Point (x y) #:transparent)
(define data
- (for/fold ([cells (hash)])
- ([row (in-lines (open-day 15 2021))]
- [x (in-naturals)])
- (for/fold ([cells cells])
- ([n (in-string row)]
- [y (in-naturals)])
- (hash-set cells
- (Point x y)
- (~> n string string->number)))))
+ (for/fold ([cells (hash)]) ([row (in-lines (open-day 15 2021))] [x (in-naturals)])
+ (for/fold ([cells cells]) ([n (in-string row)] [y (in-naturals)])
+ (hash-set cells (Point x y) (~> n string string->number)))))
(define x-max (~>> data hash-keys (map Point-x) (apply max)))
(define y-max (~>> data hash-keys (map Point-y) (apply max)))
(define (neighbors pt d)
(match-define (Point x y) pt)
- (~>> (list (Point (add1 x) y)
- (Point (sub1 x) y)
- (Point x (add1 y))
- (Point x (sub1 y)))
+ (~>> (list (Point (add1 x) y) (Point (sub1 x) y) (Point x (add1 y)) (Point x (sub1 y)))
(filter (curry hash-has-key? d))))
(define (grid-graph d)
- (weighted-graph/directed
- (for*/list ([coord (in-list (hash-keys d))]
- [neighbor (in-list (neighbors coord d))]
- [weight (in-value (hash-ref d neighbor))])
- (list weight coord neighbor))))
+ (weighted-graph/directed (for*/list ([coord (in-list (hash-keys d))]
+ [neighbor (in-list (neighbors coord d))]
+ [weight (in-value (hash-ref d neighbor))])
+ (list weight coord neighbor))))
;; part 1
(define (find-path-weight d)
@@ -42,28 +32,18 @@
(define ym (~>> d hash-keys (map Point-y) (apply max)))
(hash-ref node-distances (Point xm ym))))
-(~> data
- find-path-weight
- time)
+(~> data find-path-weight time)
;; part 2
-(define nine-cycle
- (in-cycle (inclusive-range 1 9)))
+(define nine-cycle (in-cycle (inclusive-range 1 9)))
(define (expand-data data)
- (for/fold ([cells (hash)])
- ([coord (in-list (hash-keys data))])
+ (for/fold ([cells (hash)]) ([coord (in-list (hash-keys data))])
(match-define (Point x y) coord)
(for*/fold ([cells cells])
- ([n (in-range 5)]
- [m (in-range 5)]
- [val (in-value (hash-ref data coord))])
+ ([n (in-range 5)] [m (in-range 5)] [val (in-value (hash-ref data coord))])
(hash-set cells
- (Point (+ x (* n (add1 x-max)))
- (+ y (* m (add1 y-max))))
+ (Point (+ x (* n (add1 x-max))) (+ y (* m (add1 y-max))))
(sequence-ref nine-cycle (+ val n m -1))))))
-(~> data
- expand-data
- find-path-weight
- time) \ No newline at end of file
+(~> data expand-data find-path-weight time)
diff --git a/2021/day-16/day-16.rkt b/2021/day-16/day-16.rkt
index 4183ab9..86083ef 100644
--- a/2021/day-16/day-16.rkt
+++ b/2021/day-16/day-16.rkt
@@ -3,39 +3,27 @@
bitsyntax
threading)
-(struct packet (version type type-id contents len)
- #:transparent)
+(struct packet (version type type-id contents len) #:transparent)
(define (BITS->bitstring str)
- (integer->bit-string (string->number str 16)
- (* 4 (string-length str))
- #true))
+ (integer->bit-string (string->number str 16) (* 4 (string-length str)) #true))
-(define data
- (~> (open-day 16 2021)
- port->string
- string-trim
- BITS->bitstring))
+(define data (~> (open-day 16 2021) port->string string-trim BITS->bitstring))
(define (get-literal-contents bitstr)
(for/fold ([assembled (bit-string)]
[remaining bitstr]
[total-length 6]
[complete? #f]
- #:result (values (bit-string->integer assembled #t #f)
- remaining
- total-length))
- ([_ (in-naturals)]
- #:break complete?)
+ #:result (values (bit-string->integer assembled #t #f) remaining total-length))
+ ([_ (in-naturals)] #:break complete?)
(bit-string-case remaining
([(= 1 :: bits 1) (number :: bits 4) (remaining :: binary)]
(values (bit-string-append assembled (integer->bit-string number 4 #t))
remaining
(+ total-length 5)
#f))
- ([(= 0 :: bits 1)
- (number :: bits 4)
- (remaining :: binary)]
+ ([(= 0 :: bits 1) (number :: bits 4) (remaining :: binary)]
(values (bit-string-append assembled (integer->bit-string number 4 #t))
remaining
(+ total-length 5)
@@ -43,67 +31,50 @@
(define (get-type-0-contents cnt bitstr [acc '()] [len 0])
(cond
- [(<= cnt 0) (values (reverse acc)
- bitstr
- len)]
- [else (define-values (packet remaining)
- (identify-next-packet bitstr))
- (get-type-0-contents (- cnt (packet-len packet))
- remaining
- (cons packet acc)
- (+ len (packet-len packet)))]))
+ [(<= cnt 0) (values (reverse acc) bitstr len)]
+ [else
+ (define-values (packet remaining) (identify-next-packet bitstr))
+ (get-type-0-contents (- cnt (packet-len packet))
+ remaining
+ (cons packet acc)
+ (+ len (packet-len packet)))]))
(define (get-type-1-contents cnt bitstr [acc '()] [len 0])
(cond
- [(= cnt 0) (values (reverse acc)
- bitstr
- len)]
- [else (define-values (packet remaining)
- (identify-next-packet bitstr))
- (get-type-1-contents (sub1 cnt)
- remaining
- (cons packet acc)
- (+ len (packet-len packet)))]))
+ [(= cnt 0) (values (reverse acc) bitstr len)]
+ [else
+ (define-values (packet remaining) (identify-next-packet bitstr))
+ (get-type-1-contents (sub1 cnt) remaining (cons packet acc) (+ len (packet-len packet)))]))
(define (identify-next-packet bitstr)
- (bit-string-case bitstr
- ([(packet-version :: bits 3)
- (= 4 :: bits 3)
- (remaining :: binary)]
- (define-values (n now-remaining len)
- (get-literal-contents remaining))
- (values (packet packet-version 'literal 4 n len)
- now-remaining))
-
- ([(packet-version :: bits 3)
- (type-id :: bits 3)
- (= 0 :: bits 1)
- (subpacket-length :: bits 15)
- (remaining :: binary)]
- (define-values (contents now-remaining sublength)
- (get-type-0-contents subpacket-length remaining))
- (values (packet packet-version 'operator type-id contents (+ 22 sublength))
- now-remaining))
-
- ([(packet-version :: bits 3)
- (type-id :: bits 3)
- (= 1 :: bits 1)
- (subpacket-count :: bits 11)
- (remaining :: binary)]
- (define-values (contents now-remaining sublength)
- (get-type-1-contents subpacket-count remaining))
- (values (packet packet-version 'operator type-id contents (+ 22 sublength))
- now-remaining))))
-
-(match-define-values (outer-packet n)
- (identify-next-packet data))
+ (bit-string-case
+ bitstr
+ ([(packet-version :: bits 3) (= 4 :: bits 3) (remaining :: binary)]
+ (define-values (n now-remaining len) (get-literal-contents remaining))
+ (values (packet packet-version 'literal 4 n len) now-remaining))
+ ([(packet-version :: bits 3)
+ (type-id :: bits 3)
+ (= 0 :: bits 1)
+ (subpacket-length :: bits 15)
+ (remaining :: binary)]
+ (define-values (contents now-remaining sublength)
+ (get-type-0-contents subpacket-length remaining))
+ (values (packet packet-version 'operator type-id contents (+ 22 sublength)) now-remaining))
+ ([(packet-version :: bits 3)
+ (type-id :: bits 3)
+ (= 1 :: bits 1)
+ (subpacket-count :: bits 11)
+ (remaining :: binary)]
+ (define-values (contents now-remaining sublength) (get-type-1-contents subpacket-count remaining))
+ (values (packet packet-version 'operator type-id contents (+ 22 sublength)) now-remaining))))
+
+(match-define-values (outer-packet n) (identify-next-packet data))
;; part 1
(define (packet-sum-version p)
(match p
[(packet v 'literal _type-id _contents _len) v]
- [(packet v 'operator _type-id ps _len)
- (foldl (λ (p acc) (+ acc (packet-sum-version p))) v ps)]))
+ [(packet v 'operator _type-id ps _len) (foldl (λ (p acc) (+ acc (packet-sum-version p))) v ps)]))
(packet-sum-version outer-packet)
@@ -121,10 +92,6 @@
(define packet-eval
(match-lambda
[(packet _v 'literal _type-id n _len) n]
- [(packet _v 'operator type-id ps _len)
- (~>> ps
- (map packet-eval)
- (apply (packet-f type-id)))]))
+ [(packet _v 'operator type-id ps _len) (~>> ps (map packet-eval) (apply (packet-f type-id)))]))
(packet-eval outer-packet)
-
diff --git a/2021/day-17/day-17.rkt b/2021/day-17/day-17.rkt
index b8563a5..7de44a0 100644
--- a/2021/day-17/day-17.rkt
+++ b/2021/day-17/day-17.rkt
@@ -4,38 +4,30 @@
(define-values (x-min x-max y-min y-max)
(~> (open-day 17 2021)
- port->string
- (regexp-match #px"target area: x=(.*)\\.\\.(.*), y=(.*)\\.\\.(.*)\n" _)
- rest
- (map string->number _)
- (apply values _)))
+ port->string
+ (regexp-match #px"target area: x=(.*)\\.\\.(.*), y=(.*)\\.\\.(.*)\n" _)
+ rest
+ (map string->number _)
+ (apply values _)))
(define (hit? x y)
- (and (x . >= . x-min)
- (x . <= . x-max)
- (y . >= . y-min)
- (y . <= . y-max)))
+ (and (x . >= . x-min) (x . <= . x-max) (y . >= . y-min) (y . <= . y-max)))
(define (miss? x y)
- (or (y . < . y-min)
- (x . > . x-max)))
+ (or (y . < . y-min) (x . > . x-max)))
-(define (drag dx i) (max (- dx i) 0))
-(define (gravity dy i) (- dy i))
+(define (drag dx i)
+ (max (- dx i) 0))
+(define (gravity dy i)
+ (- dy i))
(define (find-trajectory-apex dx dy)
- (for/fold ([x 0] [y 0] [y-apex 0] [result #f]
- #:result (list y-apex result))
- ([i (in-naturals)]
- #:break result)
+ (for/fold ([x 0] [y 0] [y-apex 0] [result #f] #:result (list y-apex result))
+ ([i (in-naturals)] #:break result)
(cond
[(hit? x y) (values dx dy y-apex 'hit)]
[(miss? x y) (values x y 'miss 'miss)]
- [else
- (values (+ x (drag dx i))
- (+ y (gravity dy i))
- (if (y . > . y-apex) y y-apex)
- #f)])))
+ [else (values (+ x (drag dx i)) (+ y (gravity dy i)) (if (y . > . y-apex) y y-apex) #f)])))
(define on-target
(for*/list ([dx (in-inclusive-range 1 x-max)]
@@ -45,9 +37,7 @@
(list dx dy (first velocity))))
;; part 1
-(~>> on-target
- (argmax third)
- third)
+(~>> on-target (argmax third) third)
;; part 2
-(length on-target) \ No newline at end of file
+(length on-target)
diff --git a/2021/day-19/day-19.rkt b/2021/day-19/day-19.rkt
index b05c56b..4c6334d 100644
--- a/2021/day-19/day-19.rkt
+++ b/2021/day-19/day-19.rkt
@@ -1,6 +1,5 @@
#lang racket
(require "../../jj-aoc.rkt"
- awesome-list
threading
racket/struct)
@@ -12,10 +11,7 @@
(coord (f x1 x2) (f y1 y2) (f z1 z2)))
(define (coord-reduce f c1 c2)
- (foldl (λ (i1 i2 acc) (+ acc (f i1 i2)))
- 0
- (struct->list c1)
- (struct->list c2)))
+ (foldl (λ (i1 i2 acc) (+ acc (f i1 i2))) 0 (struct->list c1) (struct->list c2)))
(define coord-delta (curry coord-broadcast -))
(define coord-sum (curry coord-broadcast +))
@@ -23,42 +19,30 @@
(define (create-scan-data d)
(for/list ([l (in-list d)])
- (for/list ([pt (in-list (~> (string-split l "\n")
- rest))])
+ (for/list ([pt (in-list (~> (string-split l "\n") rest))])
(~>> pt
(regexp-match #px"(-?\\d+),(-?\\d+),(-?\\d+)")
rest
(map string->number)
(apply coord)))))
-(define scanner-data (create-scan-data (~>> (open-day 19 2021)
- port->string
- (string-split _ "\n\n"))))
-
-(define test-scanner-data (create-scan-data (~>> (open-input-file "test-scanners")
- port->string
- (string-split _ "\n\n"))))
+(define scanner-data (create-scan-data (~>> (open-day 19 2021) port->string (string-split _ "\n\n"))))
(define (generate-rotations scanner)
- (transpose
+ (apply
+ map
+ list
(for*/list ([pt (in-list scanner)])
(match-define (coord x y z) pt)
- (define orientations (list (list x y z)
- (list x z (- y))
- (list x (- y) (- z))
- (list x (- z) y)))
- (append*
- (for/list ([o (in-list orientations)])
- (match-define (list x* y* z*) o)
- (list (list x y z)
- (list (- x) z y)
- (list z (- y) x)
- (list y x (- z))
- (list (- y) (- z) x)))))))
+ (define orientations (list (list x y z) (list x z (- y)) (list x (- y) (- z)) (list x (- z) y)))
+ (append* (for/list ([o (in-list orientations)])
+ (match-define (list x* y* z*) o)
+ (list (list x y z)
+ (list (- x) z y)
+ (list z (- y) x)
+ (list y x (- z))
+ (list (- y) (- z) x)))))))
(define (find-overlaps scan1 scan2)
(for/list ([rotation (in-permutations scan2)])
(map coord-sum scan1 rotation)))
-
-(first test-scanner-data)
-(find-overlaps (first test-scanner-data) (second test-scanner-data)) \ No newline at end of file
diff --git a/2021/day-20/day-20.rkt b/2021/day-20/day-20.rkt
index c65298e..b7ed092 100644
--- a/2021/day-20/day-20.rkt
+++ b/2021/day-20/day-20.rkt
@@ -1,36 +1,32 @@
#lang racket
(require "../../jj-aoc.rkt"
- threading
- racket/hash)
+ threading)
(struct pixel (i j) #:transparent)
(define-values (raw-enhancement raw-image)
- (~> (open-day 20 2021)
- port->string
- (string-split "\n\n")
- (apply values _)))
+ (~> (open-day 20 2021) port->string (string-split "\n\n") (apply values _)))
-(define (char->pixel c) (if (char=? #\# c) 'light 'dark))
-(define (pixel->char c) (if (equal? 'light c) #\# #\.))
+(define (char->pixel c)
+ (if (char=? #\# c) 'light 'dark))
+(define (pixel->char c)
+ (if (equal? 'light c) #\# #\.))
(define enhancement-algorithm
(for/hash ([(c i) (in-indexed (~> raw-enhancement (string-replace "\n" "")))])
(values i (char->pixel c))))
(define image-hash
- (for*/hash ([(row i) (in-indexed (string-split raw-image "\n"))]
- [(c j) (in-indexed row)])
+ (for*/hash ([(row i) (in-indexed (string-split raw-image "\n"))] [(c j) (in-indexed row)])
(values (pixel i j) (char->pixel c))))
(define (window->new-pixel p ps bg)
(match-define (pixel i j) p)
- (~> (for*/list ([di '(-1 0 1)]
- [dj '(-1 0 1)])
+ (~> (for*/list ([di '(-1 0 1)] [dj '(-1 0 1)])
(if (equal? 'dark (hash-ref ps (pixel (+ i di) (+ j dj)) bg)) #\0 #\1))
- (apply string _)
- (string->number _ 2)
- (hash-ref enhancement-algorithm _)))
+ (apply string _)
+ (string->number _ 2)
+ (hash-ref enhancement-algorithm _)))
(define (pad-hash ps bg)
(define coords (hash-keys ps))
@@ -51,16 +47,13 @@
;; the infinite background flips colors every other enhancement step
;; instead of trying to account for this algorithmically I just hardcoded it in
(~> image-hash
- (enhance-image 'dark)
- (enhance-image 'light)
- hash-values
- (count (curry equal? 'light) _))
+ (enhance-image 'dark)
+ (enhance-image 'light)
+ hash-values
+ (count (curry equal? 'light) _))
;; part 2
-(~> (for/fold ([img image-hash])
- ([_ (in-range 25)])
- (~> img
- (enhance-image 'dark)
- (enhance-image 'light)))
- hash-values
- (count (curry equal? 'light) _)) \ No newline at end of file
+(~> (for/fold ([img image-hash]) ([_ (in-range 25)])
+ (~> img (enhance-image 'dark) (enhance-image 'light)))
+ hash-values
+ (count (curry equal? 'light) _))
diff --git a/2021/day-21/day-21.rkt b/2021/day-21/day-21.rkt
index 46bbbe2..9ca9b1b 100644
--- a/2021/day-21/day-21.rkt
+++ b/2021/day-21/day-21.rkt
@@ -20,38 +20,40 @@
#:result (list (min player-1-score player-2-score) (* 3 last-turn)))
([turn-count (in-naturals 1)]
[turn-for current-turn]
- #:break (or (player-1-score . >= . 1000)
- (player-2-score . >= . 1000)))
+ #:break (or (player-1-score . >= . 1000) (player-2-score . >= . 1000)))
(define rolls (apply + (stream->list (stream-take dice 3))))
(match turn-for
- ['player-1 (define next-track (stream-tail player-1-track rolls))
- (values (+ player-1-score (stream-first next-track)) next-track
- player-2-score player-2-track
- (stream-tail dice 3)
- turn-count)]
- ['player-2 (define next-track (stream-tail player-2-track rolls))
- (values player-1-score player-1-track
- (+ player-2-score (stream-first next-track)) next-track
- (stream-tail dice 3)
- turn-count)]))
- (apply * _))
+ ['player-1
+ (define next-track (stream-tail player-1-track rolls))
+ (values (+ player-1-score (stream-first next-track))
+ next-track
+ player-2-score
+ player-2-track
+ (stream-tail dice 3)
+ turn-count)]
+ ['player-2
+ (define next-track (stream-tail player-2-track rolls))
+ (values player-1-score
+ player-1-track
+ (+ player-2-score (stream-first next-track))
+ next-track
+ (stream-tail dice 3)
+ turn-count)]))
+ (apply * _))
;; part 2
(define d3 (list 1 2 3))
-(define roll-space (~>> (cartesian-product d3 d3 d3)
- (map (λ~>> (apply +)))))
+(define roll-space (~>> (cartesian-product d3 d3 d3) (map (λ~>> (apply +)))))
-(define/memo (next-turns p1-score p2-score p1-start p2-start)
- (cond
- [(p1-score . >= . 21) '(1 0)]
- [(p2-score . >= . 21) '(0 1)]
- [else
- (for/fold ([wins '(0 0)])
- ([roll (in-list roll-space)])
- (define move-to (add1 (modulo (sub1 (+ roll p1-start)) 10)))
- (define possible-wins (next-turns p2-score (+ p1-score move-to) p2-start move-to))
- (list (+ (first wins) (second possible-wins))
- (+ (second wins) (first possible-wins ))))]))
+(define/memo
+ (next-turns p1-score p2-score p1-start p2-start)
+ (cond
+ [(p1-score . >= . 21) '(1 0)]
+ [(p2-score . >= . 21) '(0 1)]
+ [else
+ (for/fold ([wins '(0 0)]) ([roll (in-list roll-space)])
+ (define move-to (add1 (modulo (sub1 (+ roll p1-start)) 10)))
+ (define possible-wins (next-turns p2-score (+ p1-score move-to) p2-start move-to))
+ (list (+ (first wins) (second possible-wins)) (+ (second wins) (first possible-wins))))]))
-(~>> (next-turns 0 0 player-1-start player-2-start)
- (apply max)) \ No newline at end of file
+(~>> (next-turns 0 0 player-1-start player-2-start) (apply max))
diff --git a/2021/day-25/day-25.rkt b/2021/day-25/day-25.rkt
index ba18b45..7a3a5ca 100644
--- a/2021/day-25/day-25.rkt
+++ b/2021/day-25/day-25.rkt
@@ -3,43 +3,33 @@
threading)
(define sea-floor
- (for*/hash ([(row i) (in-indexed (in-lines (open-day 25 2021)))]
- [(c j) (in-indexed row)])
+ (for*/hash ([(row i) (in-indexed (in-lines (open-day 25 2021)))] [(c j) (in-indexed row)])
(values (list i j) c)))
(define-values (max-i max-j)
- (~> sea-floor
- hash-keys
- (argmax (λ (coord) (apply + coord)) _)
- (apply values _)))
+ (~> sea-floor hash-keys (argmax (λ (coord) (apply + coord)) _) (apply values _)))
(define (cucumber-movement h c delta-i delta-j)
(define new-hash (hash-copy h))
- (for* ([(coord x) (in-hash h)]
- #:when (eq? x c))
+ (for* ([(coord x) (in-hash h)] #:when (eq? x c))
(match-define (list i j) coord)
(define neighbor (list (+ delta-i i) (+ delta-j j)))
- (define neighbor-or-wrap (if (hash-has-key? h neighbor)
- neighbor
- (list (if (= delta-i 0) i 0)
- (if (= delta-j 0) j 0))))
+ (define neighbor-or-wrap
+ (if (hash-has-key? h neighbor) neighbor (list (if (= delta-i 0) i 0) (if (= delta-j 0) j 0))))
(when (eq? #\. (hash-ref h neighbor-or-wrap))
(hash-set*! new-hash coord #\. neighbor-or-wrap c)))
new-hash)
-(define (eastwards-movement h) (cucumber-movement h #\> 0 1))
+(define (eastwards-movement h)
+ (cucumber-movement h #\> 0 1))
-(define (southwards-movement h) (cucumber-movement h #\v 1 0))
+(define (southwards-movement h)
+ (cucumber-movement h #\v 1 0))
;; part 1
-(for/fold ([f sea-floor]
- [step 0]
- #:result (add1 step))
- ([i (in-naturals 1)])
- (define f* (~> f
- eastwards-movement
- southwards-movement))
+(for/fold ([f sea-floor] [step 0] #:result (add1 step)) ([i (in-naturals 1)])
+ (define f* (~> f eastwards-movement southwards-movement))
#:break (equal? f* f)
(values f* i))
-;; no part 2 -- merry Christmas \ No newline at end of file
+;; no part 2 -- merry Christmas