Skip to content

Commit

Permalink
prepare for Qi with bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
benknoble committed Dec 25, 2022
1 parent 8ec6a23 commit f20d052
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 15 deletions.
5 changes: 3 additions & 2 deletions day1/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

(provide (all-defined-out))

(require qi)
(require qi
(prefix-in list: racket/list))

(define-flow partn (~>> (-< drop-right drop) (count <)))
(define-flow partn (~>> (-< drop-right drop) (list:count <)))

(define-flow part1* (partn 1))
(define-flow part1 (~> file->list part1*))
Expand Down
6 changes: 4 additions & 2 deletions day10/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@

(define checker-scores (hash #\) 3 #\] 57 #\} 1197 #\> 25137))

(define checker (make-checker closers))

(define-flow part1*
(~> sep
(amp (make-checker closers))
(amp checker)
(pass char?)
(amp (hash-ref checker-scores _))
+))
(define-flow part1 (~> file->lines part1*))

(define-flow incomplete?
(~> (make-checker closers)
(~> checker
(if (or char? null?) ground _)))

(define complete-scores (hash #\) 1 #\] 2 #\} 3 #\> 4))
Expand Down
2 changes: 1 addition & 1 deletion day11/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
(define (stepN n) (flow (feedback n step)))

(define-flow part1*
(~> (stepN 100) 2>))
(~> (esc (stepN 100)) 2>))
(define-flow part1 (~> file->grid part1*))

(define-flow part2*
Expand Down
4 changes: 2 additions & 2 deletions day13/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
(match instruction
[`(x ,n)
(~> (points)
(set-map (flow (~> (-< x y) (update-first n) cons)))
(set-map (flow (~> (-< x y) (esc (update-first n)) cons)))
sep set)]
[`(y ,n)
(~> (points)
(set-map (flow (~> (-< y x) (update-first n) X cons)))
(set-map (flow (~> (-< y x) (esc (update-first n)) X cons)))
sep set)]))

(define (fold instructions points)
Expand Down
2 changes: 1 addition & 1 deletion day16/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
(define-values (data offset)
(for/fold ([data null]
[offset (+ 11 1 start)])
([_ (in-range length)])
([_ignore (in-range length)])
(~>> (offset)
(decode packet-string)
(== (cons data) _))))
Expand Down
5 changes: 3 additions & 2 deletions day20/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

(provide (all-defined-out))

(require qi)
(require qi
(prefix-in list: racket/list))

(define-flow light? (or (eq? 1) (eq? #\#)))
(define-flow dark? (or (eq? 0) (eq? #\.)))
Expand Down Expand Up @@ -102,7 +103,7 @@
(sub1 ym) (add1 yM))))

(define-flow count-light
(~>> hash-values (count light?)))
(~>> hash-values (list:count light?)))

(define (count-light-n n)
(flow (~>> (enhance-image-n n) 1> (ε display-image count-light))))
Expand Down
2 changes: 1 addition & 1 deletion day3/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
(define-flow (fieldth field) (clos (~> X list-ref)))
(define (criterion-solver selector rows)
(define (filterc field criterion rows)
(define-flow matches (~> (fieldth field) (= criterion)))
(define-flow matches (~> (esc (fieldth field)) (= criterion)))
(filter matches rows))
;; we cannot name this loop, because qi's flow has a loop syntactic form that
;; confuses our intent.
Expand Down
2 changes: 1 addition & 1 deletion day4/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
(hash-ref n #f)
(if _
update-board-board
(const b))))
(gen b))))

(define (find-first-winner moves boards)
(let loop ([boards boards]
Expand Down
3 changes: 2 additions & 1 deletion day5/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(provide (all-defined-out))

(require qi
(prefix-in list: racket/list)
"../common.rkt")

(struct point [x y] #:transparent)
Expand Down Expand Up @@ -63,7 +64,7 @@
collect
(group-by identity)
(map length)
(count (flow (>= 2)))))
(list:count (flow (>= 2)))))

(define-flow part1* (~>> list->segments (filter segment-cardinal?) count-overlaps))
(define-flow part1 (~> (file->list read-ignore-comma) part1*))
Expand Down
2 changes: 1 addition & 1 deletion day8/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
(remove-duplicates (find-pats-matching-digits pats '(0 6 9))))
;; 3
(match-define-values (`(,six) zero-nine)
(partition (flow (~>> (subset? one) not)) zero-six-nine))
(partition (flow (not (subset? one _))) zero-six-nine))
;; 4
(define C (set-first (set-subtract one six)))
;; 5
Expand Down
2 changes: 1 addition & 1 deletion day9/solution.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
(if (and% (not (set-member? seen _))
(and (< height _) (not (>= 9))))
(~> (-< cons (~>> 1> (set-add seen)))
(basin-size* grid)
(esc (basin-size* grid))
(== (+ size) _))
(gen size seen)))))

Expand Down

0 comments on commit f20d052

Please sign in to comment.