adventofcode

https://adventofcode.com/
Log | Files | Refs

commit 5b9a99f85f1e606e1db6ff7dfbd99e0951511cf8
parent 707e673e9567cbd03dcd56bb3addb7e10bf1deeb
Author: mpizzzle <m@michaelpercival.xyz>
Date:   Wed,  2 Dec 2020 23:01:12 +0000

refactoring read-lines into seperate file

Diffstat:
M2020/puzzle_1.scm | 18+++++-------------
M2020/puzzle_2.scm | 29++++++++++-------------------
A2020/read_lines.scm | 9+++++++++
3 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/2020/puzzle_1.scm b/2020/puzzle_1.scm @@ -1,13 +1,3 @@ -(define (read-lines . args) - (let ((p (cond ((string? (car args)) (open-input-file (car args)))))) - (let loop ((line (read-line p)) (lines (list))) - (if (eof-object? line) - (begin - (if (and (pair? args) (string? (car args))) - (close-input-port p)) - (reverse lines)) - (loop (read-line p) (cons (string->number line) lines)))))) - (define (part-1 x a b) (if (and (not (null? a)) (= 2020 (+ (car a) (car b)))) (list (car a) (car b) (* (car a) (car b))) @@ -28,6 +18,8 @@ (part-2 x x (cdr b) c)) (part-2 x (cdr a) b c)))) -(define i (read-lines "files/1.txt")) -(part-1 i i i) -(part-2 i i i i) +(load "read_lines.scm") +(define i (read-lines "files/1.txt" (lambda (x) (string->number x)))) + +(display (part-1 i i i)) (newline) +(display (part-2 i i i i)) (newline) diff --git a/2020/puzzle_2.scm b/2020/puzzle_2.scm @@ -1,13 +1,3 @@ -(define (read-lines . args) - (let ((p (cond ((string? (car args)) (open-input-file (car args)))))) - (let loop ((line (read-line p)) (lines (list))) - (if (eof-object? line) - (begin - (if (and (pair? args) (string? (car args))) - (close-input-port p)) - (reverse lines)) - (loop (read-line p) (cons line lines)))))) - (define (string-index str chr idx) (if (not (null? str)) (if (char=? (car (string->list str)) chr) @@ -15,12 +5,6 @@ (string-index (list->string (cdr (string->list str))) chr (+ idx 1))) -1)) -(define (char-count str chr count) - (if (not (= 0 (string-length str))) - (char-count (list->string (cdr (string->list str))) chr - (if (char=? (car (string->list str)) chr)(+ count 1) count)) - count)) - (define (valid-password entry policy) (let ((idx (string-index entry #- 0)) @@ -38,6 +22,12 @@ (loop (cdr entries) policy)) 0)) +(define (char-count str chr count) + (if (not (= 0 (string-length str))) + (char-count (list->string (cdr (string->list str))) chr + (if (char=? (car (string->list str)) chr)(+ count 1) count)) + count)) + (define policy-1 (lambda (s c i j) (let ((count (char-count s c 0))) @@ -48,7 +38,8 @@ (if (not (equal? (char=? (string-ref s (- i 1)) c) (char=? (string-ref s (- j 1)) c))) 1 0))) -(define input (read-lines "files/2.txt")) +(load "read_lines.scm") +(define input (read-lines "files/2.txt" (lambda (x) x))) -(loop input policy-1) -(loop input policy-2) +(display (loop input policy-1)) (newline) +(display (loop input policy-2)) (newline) diff --git a/2020/read_lines.scm b/2020/read_lines.scm @@ -0,0 +1,9 @@ +(define (read-lines file detail) + (let ((infile (open-input-file file))) + (let loop ((lines '()) + (next-line (read-line infile))) + (if (eof-object? next-line) + (begin (close-input-port infile) + (reverse lines)) + (loop (cons (detail next-line) lines) + (read-line infile))))))