day 1, part 2, simplified tokenize

main
Inga 🏳‍🌈 12 months ago
parent 8963e32886
commit 5f8c81daf2
  1. 34
      day01-hard/main.scm

@ -21,6 +21,8 @@
(define (is-not value) (lambda (x) (not (= x value))))
(define is-not-empty (lambda (x) (not (null? x))))
(define (prepend first-value) (lambda (rest) (cons first-value rest)))
(define Y (lambda (f) (f (lambda (x) ((Y f) x)))))
(define Y2 (lambda (f) (f (lambda (x y) ((Y2 f) x y)))))
@ -60,6 +62,10 @@
'(1 2 3)
((first is-not-empty) '(() (1 2 3))))
(define (coalesce-not-empty default) (lambda (value) ((first is-not-empty) (list value default))))
(define (truthy-chaining f) (lambda (value) (if value (f value) #f)))
(define (compose-two f g) (lambda (x) (f (g x))))
(define compose (reduce-right compose-two id))
@ -73,7 +79,6 @@
(lambda (f) (lambda (n)
(if (= n 0) '() (cons value (f (- n 1))))))))
;=============== starts-with ======================
(define starts-with (Y2
(lambda (f) (lambda (prefix values)
@ -113,23 +118,17 @@
(if
(null? values)
'(())
((first is-not-empty) (list
(flat
((filter id)
((map
((compose (list
(coalesce-not-empty (f (cdr values)))
flat
(filter id)
(map
(lambda (token)
((lambda (rest)
(if
rest
((map
(lambda (rest-tokenized)
(cons (car token) rest-tokenized)))
(f (next rest values)))
#f))
(starts-with (cdr token) values))))
tokens)))
(f (cdr values))
'((77 78) (79 76)))))))))
((truthy-chaining (compose (list
(map (prepend (car token)))
(lambda (rest) (f (next rest values))))))
(starts-with (cdr token) values))))))
tokens))))))
(define (tokenize tokens)
(tokenize-generic tokens (lambda (rest values) rest)))
@ -151,6 +150,7 @@
(define (tokenize-aoc tokens)
(tokenize-generic tokens (lambda (rest values) (cdr values))))
;=============== solution ======================
(define solution-tokens
(list

Loading…
Cancel
Save