diff --git a/.gitignore b/.gitignore index 99afe76..384aa48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -inputs +input diff --git a/main.rkt b/main.rkt index 3b96818..fcbff0b 100644 --- a/main.rkt +++ b/main.rkt @@ -4,9 +4,8 @@ (require "src/day01.rkt") (define (run-all) - (printf "Running all solutions") - (define result-01-p1 (advent-of-code-01-p1)) - (printf (string-append "Day 1 part 1: " result-01-p1))) + (printf "Running all solutions\n") + (printf (string-append "Day 1 part 1: " (advent-of-code-01-p1)))) (define argv (current-command-line-arguments)) diff --git a/src/day01.rkt b/src/day01.rkt index f6f2910..351676c 100644 --- a/src/day01.rkt +++ b/src/day01.rkt @@ -49,6 +49,7 @@ current-char (get-first-digit (rest list)))))) +; ?? (define (get-last-digit list carry) (if (= (length list) 0) carry @@ -59,23 +60,23 @@ carry)]) (get-last-digit (rest list) next-char)))) -; Creates a new number from the first & last number found inside a string +; Creates a new number from the first & last number found inside a char list (define (list->digits list) (let ([first-char (get-first-digit list)] [last-char (get-last-digit list -1)]) (string->number (string first-char last-char)))) + (define (advent-of-code-01-p1) ; Read file (define file-contents - (port->string (open-input-file "input-01.txt") #:close? #t)) + (port->string (open-input-file "input/01.txt") #:close? #t)) ; Split file into list of strings - (define input (string-split file-contents "\n")) - - ; process - (define char-list (map string->list input)) - (define result (foldl + 0 (map list->digits char-list))) - result) + (define lines (string-split file-contents "\n")) + (define list-of-list-of-chars (map string->list lines)) + (define list-of-digits (map list->digits list-of-list-of-chars)) + (define sum (foldl + 0 list-of-digits)) + (number->string sum))