From 8f6053e781eaa6c8b1f497c5a7e58af23e2dcb7f Mon Sep 17 00:00:00 2001 From: Araozu Date: Thu, 31 Oct 2024 06:11:47 -0500 Subject: [PATCH] refactor: day 1 part 1 --- src/day01.rkt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/day01.rkt b/src/day01.rkt index 351676c..6731e7b 100644 --- a/src/day01.rkt +++ b/src/day01.rkt @@ -41,15 +41,18 @@ ; From a char list returns the first digit it finds as a char, or nil +; char list -> char (define (get-first-digit list) - (if (= (length list) 0) - null - (let ([current-char (first list)]) - (if (char-numeric? current-char) + (if (empty? list) + null + (let ([current-char (first list)]) + (if (char-numeric? current-char) current-char (get-first-digit (rest list)))))) -; ?? + +; finds the last digit in the given list +; char list -> char (define (get-last-digit list carry) (if (= (length list) 0) carry @@ -61,6 +64,7 @@ (get-last-digit (rest list) next-char)))) ; Creates a new number from the first & last number found inside a char list +; char list -> int (define (list->digits list) (let ([first-char (get-first-digit list)] [last-char (get-last-digit list -1)]) @@ -80,3 +84,7 @@ (define sum (foldl + 0 list-of-digits)) (number->string sum)) + +(define (advent-of-code-01-p2) + 0) +