From f17f9dc81c8308c51e51de4725b25f10e2e9ecd6 Mon Sep 17 00:00:00 2001 From: MsK` Date: Tue, 7 Dec 2021 11:21:21 +0100 Subject: [PATCH] move common code to common.scm --- 04-giant-squid/code.scm | 12 ++---------- 06-lanterfish/code.scm | 11 +---------- common.scm | 9 +++++++++ 3 files changed, 12 insertions(+), 20 deletions(-) create mode 100644 common.scm diff --git a/04-giant-squid/code.scm b/04-giant-squid/code.scm index c634f95..9216af8 100644 --- a/04-giant-squid/code.scm +++ b/04-giant-squid/code.scm @@ -1,14 +1,6 @@ (import (srfi :43)) -; returns a list of numbers parsed from the first line of the file, -; separated by commas -(define (parse-draws file) - (let loop [(draws '()) (n 0)] - (let [(c (get-char file))] - (cond - [(char=? c #\,) (loop (cons n draws) 0)] - [(char-whitespace? c) (reverse (cons n draws))] - [else (loop draws (+ (* n 10) (string->number (string c))))])))) +(load "../common.scm") ; returns numbers 0 to 9 from ascii character (define (char->number c) @@ -140,7 +132,7 @@ (call-with-input-file "input" (lambda (file) - (let* [(draws (parse-draws file)) + (let* [(draws (read-comma-separated-numbers file)) (boards (read-boards file))] (printf "part 1: ~% ") (let* [(marks (make-vector (length boards) 0)) diff --git a/06-lanterfish/code.scm b/06-lanterfish/code.scm index 54b1687..ffe2cd0 100644 --- a/06-lanterfish/code.scm +++ b/06-lanterfish/code.scm @@ -1,14 +1,5 @@ (import (srfi :43)) ; vector extensions - -; returns a list of numbers parsed from the first line of the file, -; separated by commas -(define (read-comma-separated-numbers file) - (let loop [(draws '()) (n 0)] - (let [(c (get-char file))] - (cond - [(char=? c #\,) (loop (cons n draws) 0)] - [(char-whitespace? c) (reverse (cons n draws))] - [else (loop draws (+ (* n 10) (string->number (string c))))])))) +(load "../common.scm") ;; naive version, as per the example ; simulate lanterfish growth and returns the number of fishes after given days diff --git a/common.scm b/common.scm new file mode 100644 index 0000000..e839719 --- /dev/null +++ b/common.scm @@ -0,0 +1,9 @@ +; returns a list of numbers parsed from the first line of the file, +; separated by commas +(define (read-comma-separated-numbers file) + (let loop [(draws '()) (n 0)] + (let [(c (get-char file))] + (cond + [(char=? c #\,) (loop (cons n draws) 0)] + [(char-whitespace? c) (reverse (cons n draws))] + [else (loop draws (+ (* n 10) (string->number (string c))))]))))