move common code to common.scm
This commit is contained in:
@@ -1,14 +1,6 @@
|
|||||||
(import (srfi :43))
|
(import (srfi :43))
|
||||||
|
|
||||||
; returns a list of numbers parsed from the first line of the file,
|
(load "../common.scm")
|
||||||
; 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))))]))))
|
|
||||||
|
|
||||||
; returns numbers 0 to 9 from ascii character
|
; returns numbers 0 to 9 from ascii character
|
||||||
(define (char->number c)
|
(define (char->number c)
|
||||||
@@ -140,7 +132,7 @@
|
|||||||
(call-with-input-file
|
(call-with-input-file
|
||||||
"input"
|
"input"
|
||||||
(lambda (file)
|
(lambda (file)
|
||||||
(let* [(draws (parse-draws file))
|
(let* [(draws (read-comma-separated-numbers file))
|
||||||
(boards (read-boards file))]
|
(boards (read-boards file))]
|
||||||
(printf "part 1: ~% ")
|
(printf "part 1: ~% ")
|
||||||
(let* [(marks (make-vector (length boards) 0))
|
(let* [(marks (make-vector (length boards) 0))
|
||||||
|
|||||||
@@ -1,14 +1,5 @@
|
|||||||
(import (srfi :43)) ; vector extensions
|
(import (srfi :43)) ; vector extensions
|
||||||
|
(load "../common.scm")
|
||||||
; 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))))]))))
|
|
||||||
|
|
||||||
;; naive version, as per the example
|
;; naive version, as per the example
|
||||||
; simulate lanterfish growth and returns the number of fishes after given days
|
; simulate lanterfish growth and returns the number of fishes after given days
|
||||||
|
|||||||
9
common.scm
Normal file
9
common.scm
Normal file
@@ -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))))]))))
|
||||||
Reference in New Issue
Block a user