refactor: day 1 part 1

This commit is contained in:
Araozu 2024-10-31 06:11:47 -05:00
parent 05a8044400
commit 8f6053e781

View File

@ -41,15 +41,18 @@
; From a char list returns the first digit it finds as a char, or nil ; From a char list returns the first digit it finds as a char, or nil
; char list -> char
(define (get-first-digit list) (define (get-first-digit list)
(if (= (length list) 0) (if (empty? list)
null null
(let ([current-char (first list)]) (let ([current-char (first list)])
(if (char-numeric? current-char) (if (char-numeric? current-char)
current-char current-char
(get-first-digit (rest list)))))) (get-first-digit (rest list))))))
; ??
; finds the last digit in the given list
; char list -> char
(define (get-last-digit list carry) (define (get-last-digit list carry)
(if (= (length list) 0) (if (= (length list) 0)
carry carry
@ -61,6 +64,7 @@
(get-last-digit (rest list) next-char)))) (get-last-digit (rest list) next-char))))
; Creates a new number from the first & last number found inside a char list ; Creates a new number from the first & last number found inside a char list
; char list -> int
(define (list->digits list) (define (list->digits list)
(let ([first-char (get-first-digit list)] (let ([first-char (get-first-digit list)]
[last-char (get-last-digit list -1)]) [last-char (get-last-digit list -1)])
@ -80,3 +84,7 @@
(define sum (foldl + 0 list-of-digits)) (define sum (foldl + 0 list-of-digits))
(number->string sum)) (number->string sum))
(define (advent-of-code-01-p2)
0)