M day-2-task-1-strategy-result.rkt => day-2-task-1-strategy-result.rkt +3 -1
@@ 1,6 1,8 @@
#!/usr/bin/env racket
#lang racket
+(require "utils/input.rkt")
+
(define (sum terms)
(if (null? terms) 0 (+ (car terms) (sum (cdr terms)))))
@@ 33,4 35,4 @@
(if (eq? line eof) null (cons line (lines-to-list in)))
)
-(call-with-input-file "day-2-input" (lambda (in) (rps (lines-to-list in))))
+(call-with-input-file-as-list "day-2-input" rps)
M day-2-task-2-strategy-result.rkt => day-2-task-2-strategy-result.rkt +5 -3
@@ 1,6 1,8 @@
#!/usr/bin/env racket
#lang racket
+(require "utils/input.rkt")
+
(define (sum terms)
(if (null? terms) 0 (+ (car terms) (sum (cdr terms)))))
@@ 11,8 13,8 @@
(+ 6 (m3p1 theirplay)))
(define (draw theirplay)
- (+ 3 (m3p1 (- theirplay 1)))) ; yes this is redundant
- ; it looks cute though
+ (+ 3 (m3p1 (- theirplay 1)))) ; yes this is redundant
+ ; it looks cute though
(define (lose theirplay)
(+ 0 (m3p1 (+ theirplay 1))))
@@ 38,4 40,4 @@
(if (eq? line eof) null (cons line (lines-to-list in)))
)
-(call-with-input-file "day-2-input" (lambda (in) (rps (lines-to-list in))))
+(call-with-input-file-as-list "day-2-input" rps)
A utils/input.rkt => utils/input.rkt +16 -0
@@ 0,0 1,16 @@
+#lang racket
+
+(provide call-with-input-file-as-list)
+
+(define (call-with-input-file-as-list infile fn)
+ (call-with-input-file
+ infile
+ (lambda (in) (fn (lines-to-list in)))
+ )
+)
+
+(define (lines-to-list in)
+ (define line (read-line in))
+ (if (eq? line eof) null (cons line (lines-to-list in)))
+)
+