day 1, part 2 (works now)

main
Inga 🏳‍🌈 12 months ago
parent 5ed8b1729e
commit 8963e32886
  1. 58
      day01-hard/main.scm
  2. 7
      day01-hard/sample2.in

@ -15,6 +15,8 @@
(display "\n")) (display "\n"))
'())) '()))
(define (tee value) (car (list value (display value) (display "\n"))))
(define (id value) value) (define (id value) value)
(define (is-not value) (lambda (x) (not (= x value)))) (define (is-not value) (lambda (x) (not (= x value))))
(define is-not-empty (lambda (x) (not (null? x)))) (define is-not-empty (lambda (x) (not (null? x))))
@ -106,7 +108,7 @@
(starts-with (string->list "abc") (string->list "abcd"))) (starts-with (string->list "abc") (string->list "abcd")))
;=============== tokenize ====================== ;=============== tokenize ======================
(define (tokenize tokens) (Y (define (tokenize-generic tokens next) (Y
(lambda (f) (lambda (values) (lambda (f) (lambda (values)
(if (if
(null? values) (null? values)
@ -122,13 +124,16 @@
((map ((map
(lambda (rest-tokenized) (lambda (rest-tokenized)
(cons (car token) rest-tokenized))) (cons (car token) rest-tokenized)))
(f rest)) (f (next rest values)))
#f)) #f))
(starts-with (cdr token) values)))) (starts-with (cdr token) values))))
tokens))) tokens)))
(f (cdr values)) (f (cdr values))
'((77 78) (79 76))))))))) '((77 78) (79 76)))))))))
(define (tokenize tokens)
(tokenize-generic tokens (lambda (rest values) rest)))
(assert-eq "tokenize test 1 failed" (assert-eq "tokenize test 1 failed"
'((101 201) (101 202) (102 201) (102 202)) '((101 201) (101 202) (102 201) (102 202))
((tokenize '((101 1) (102 1) (201 2) (202 2))) ((tokenize '((101 1) (102 1) (201 2) (202 2)))
@ -144,6 +149,8 @@
((tokenize '((101 1) (102 2) (1012 1 2) (1021 2 1))) ((tokenize '((101 1) (102 2) (1012 1 2) (1021 2 1)))
'(1 2 1))) '(1 2 1)))
(define (tokenize-aoc tokens)
(tokenize-generic tokens (lambda (rest values) (cdr values))))
;=============== solution ====================== ;=============== solution ======================
(define solution-tokens (define solution-tokens
(list (list
@ -168,34 +175,53 @@
(cons #\9 (string->list "nine")))) (cons #\9 (string->list "nine"))))
(assert-eq "solution tokenize test 1 failed" (assert-eq "solution tokenize test 1 failed"
'((2 1 9)) (list (string->list "219"))
((tokenize solution-tokens) ((tokenize-aoc solution-tokens)
(string->list "two1nine"))) (string->list "two1nine")))
(assert-eq "solution tokenize test 2 failed" (assert-eq "solution tokenize test 2 failed"
'((8 2 3)) (list (string->list "823"))
((tokenize solution-tokens) ((tokenize-aoc solution-tokens)
(string->list "eightwothree"))) (string->list "eightwothree")))
(assert-eq "solution tokenize test 3 failed"
(list (string->list "123"))
((tokenize-aoc solution-tokens)
(string->list "abcone2threexyz")))
(assert-eq "solution tokenize test 4 failed"
(list (string->list "2134"))
((tokenize-aoc solution-tokens)
(string->list "xtwone3four")))
(assert-eq "solution tokenize test 5 failed"
(list (string->list "49872"))
((tokenize-aoc solution-tokens)
(string->list "4nineeightseven2")))
(assert-eq "solution tokenize test 6 failed"
(list (string->list "18234"))
((tokenize-aoc solution-tokens)
(string->list "zoneight234")))
(assert-eq "solution tokenize test 7 failed"
(list (string->list "76"))
((tokenize-aoc solution-tokens)
(string->list "7pqrstsixteen")))
(define solve-line (compose (list (define solve-line (compose (list
string->number string->number
list->string list->string
(reduce-right (reduce-right
(combine (lambda (left right) (cons (car left) (cdr right)))) (combine (lambda (left right) (cons (car left) (cdr right))))
'()) '())
(map (lambda (char) (if (char-numeric? char) ((repeat char) 2) '()))) (map (lambda (char) ((repeat char) 2)))
car
(tokenize-aoc solution-tokens)
string->list))) string->list)))
(define solve-all (compose (list (define solve-all (compose (list
sum sum
(map solve-line)))) (map solve-line))))
#!(display (solve-all (read-lines)))!# (display (solve-all (read-lines)))
#!(display (starts-with (list 5 6 7) (list 5 6)))!#
#!(display ((filter (isnot 5)) (list 3 4 5 6 7)))!#
#!(display (flat (list (list 1 2 3) (list 4 5 6) (list 7 8))))!#
#!(assert-eq "mew" (list 1 2 (list 3 4)) (list 1 2 (list 3 4)))!#

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
Loading…
Cancel
Save