From 8963e3288620a8f0838475aede767d649541fb2f Mon Sep 17 00:00:00 2001 From: Inga Date: Wed, 6 Dec 2023 00:43:26 +0000 Subject: [PATCH] day 1, part 2 (works now) --- day01-hard/main.scm | 58 +++++++++++++++++++++++++++++++------------ day01-hard/sample2.in | 7 ++++++ 2 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 day01-hard/sample2.in diff --git a/day01-hard/main.scm b/day01-hard/main.scm index c9da11f..c711299 100755 --- a/day01-hard/main.scm +++ b/day01-hard/main.scm @@ -15,6 +15,8 @@ (display "\n")) '())) +(define (tee value) (car (list value (display value) (display "\n")))) + (define (id value) value) (define (is-not value) (lambda (x) (not (= x value)))) (define is-not-empty (lambda (x) (not (null? x)))) @@ -106,7 +108,7 @@ (starts-with (string->list "abc") (string->list "abcd"))) ;=============== tokenize ====================== -(define (tokenize tokens) (Y +(define (tokenize-generic tokens next) (Y (lambda (f) (lambda (values) (if (null? values) @@ -122,13 +124,16 @@ ((map (lambda (rest-tokenized) (cons (car token) rest-tokenized))) - (f rest)) + (f (next rest values))) #f)) (starts-with (cdr token) values)))) tokens))) (f (cdr values)) '((77 78) (79 76))))))))) +(define (tokenize tokens) + (tokenize-generic tokens (lambda (rest values) rest))) + (assert-eq "tokenize test 1 failed" '((101 201) (101 202) (102 201) (102 202)) ((tokenize '((101 1) (102 1) (201 2) (202 2))) @@ -144,6 +149,8 @@ ((tokenize '((101 1) (102 2) (1012 1 2) (1021 2 1))) '(1 2 1))) +(define (tokenize-aoc tokens) + (tokenize-generic tokens (lambda (rest values) (cdr values)))) ;=============== solution ====================== (define solution-tokens (list @@ -168,34 +175,53 @@ (cons #\9 (string->list "nine")))) (assert-eq "solution tokenize test 1 failed" - '((2 1 9)) - ((tokenize solution-tokens) + (list (string->list "219")) + ((tokenize-aoc solution-tokens) (string->list "two1nine"))) (assert-eq "solution tokenize test 2 failed" - '((8 2 3)) - ((tokenize solution-tokens) + (list (string->list "823")) + ((tokenize-aoc solution-tokens) (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 string->number list->string (reduce-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))) (define solve-all (compose (list sum (map solve-line)))) -#!(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)))!# +(display (solve-all (read-lines))) diff --git a/day01-hard/sample2.in b/day01-hard/sample2.in new file mode 100644 index 0000000..4316a6b --- /dev/null +++ b/day01-hard/sample2.in @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen \ No newline at end of file