use call-with-input-file

This commit is contained in:
2021-12-02 12:44:34 +01:00
parent 11606105ea
commit ffaf4acf1e

View File

@@ -5,39 +5,41 @@
(list->string (reverse w)) (list->string (reverse w))
(loop (cons c w)))))) (loop (cons c w))))))
(let [(file (open-input-file "day-2.input"))] (call-with-input-file
(let loop [(x 0) (y 0)] "day-2.input"
(let [(amount (string->number (get-word file))) (lambda (file)
(move (get-word file))] (let loop [(x 0) (y 0)]
(cond (let [(amount (string->number (get-word file)))
((string=? move "forward") (move (get-word file))]
(loop (+ x amount) y)) (cond
((string=? move "down") ((string=? move "forward")
(loop x (+ y amount))) (loop (+ x amount) y))
((string=? move "up") ((string=? move "down")
(loop x (- y amount))) (loop x (+ y amount)))
(else ((string=? move "up")
(display "part 1: ") (loop x (- y amount)))
(display (* x y)) (else
(newline) (display "part 1: ")
(close-port file)))))) (display (* x y))
(newline)))))))
(let [(file (open-input-file "day-2.input"))] (call-with-input-file
(let loop [(x 0) (y 0) (aim 0)] "day-2.input"
(let [(amount (string->number (get-word file))) (lambda (file)
(move (get-word file))] (let loop [(x 0) (y 0) (aim 0)]
(cond (let [(amount (string->number (get-word file)))
((string=? move "forward") (move (get-word file))]
(loop (cond
(+ x amount) ((string=? move "forward")
(+ y (* aim amount)) (loop
aim)) (+ x amount)
((string=? move "down") (+ y (* aim amount))
(loop x y (+ aim amount))) aim))
((string=? move "up") ((string=? move "down")
(loop x y (- aim amount))) (loop x y (+ aim amount)))
(else ((string=? move "up")
(display "part 2: ") (loop x y (- aim amount)))
(display (* x y)) (else
(newline) (display "part 2: ")
(close-port file)))))) (display (* x y))
(newline)))))))