Compare commits
No commits in common. "8f6053e781eaa6c8b1f497c5a7e58af23e2dcb7f" and "020a953376bff7a17be2568569fe47dd2eb42032" have entirely different histories.
8f6053e781
...
020a953376
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
input
|
inputs
|
||||||
|
|
||||||
|
|
||||||
|
5
main.rkt
5
main.rkt
@ -4,8 +4,9 @@
|
|||||||
(require "src/day01.rkt")
|
(require "src/day01.rkt")
|
||||||
|
|
||||||
(define (run-all)
|
(define (run-all)
|
||||||
(printf "Running all solutions\n")
|
(printf "Running all solutions")
|
||||||
(printf (string-append "Day 1 part 1: " (advent-of-code-01-p1))))
|
(define result-01-p1 (advent-of-code-01-p1))
|
||||||
|
(printf (string-append "Day 1 part 1: " result-01-p1)))
|
||||||
|
|
||||||
|
|
||||||
(define argv (current-command-line-arguments))
|
(define argv (current-command-line-arguments))
|
||||||
|
@ -41,18 +41,14 @@
|
|||||||
|
|
||||||
|
|
||||||
; 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 (empty? list)
|
(if (= (length list) 0)
|
||||||
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
|
||||||
@ -63,28 +59,23 @@
|
|||||||
carry)])
|
carry)])
|
||||||
(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 string
|
||||||
; 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)])
|
||||||
(string->number (string first-char last-char))))
|
(string->number (string first-char last-char))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define (advent-of-code-01-p1)
|
(define (advent-of-code-01-p1)
|
||||||
; Read file
|
; Read file
|
||||||
(define file-contents
|
(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
|
; Split file into list of strings
|
||||||
(define lines (string-split file-contents "\n"))
|
(define input (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))
|
; process
|
||||||
|
(define char-list (map string->list input))
|
||||||
(define (advent-of-code-01-p2)
|
(define result (foldl + 0 (map list->digits char-list)))
|
||||||
0)
|
result)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user