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))
(loop (cons c w))))))
(let [(file (open-input-file "day-2.input"))]
(let loop [(x 0) (y 0)]
(let [(amount (string->number (get-word file)))
(move (get-word file))]
(cond
((string=? move "forward")
(loop (+ x amount) y))
((string=? move "down")
(loop x (+ y amount)))
((string=? move "up")
(loop x (- y amount)))
(else
(display "part 1: ")
(display (* x y))
(newline)
(close-port file))))))
(call-with-input-file
"day-2.input"
(lambda (file)
(let loop [(x 0) (y 0)]
(let [(amount (string->number (get-word file)))
(move (get-word file))]
(cond
((string=? move "forward")
(loop (+ x amount) y))
((string=? move "down")
(loop x (+ y amount)))
((string=? move "up")
(loop x (- y amount)))
(else
(display "part 1: ")
(display (* x y))
(newline)))))))
(let [(file (open-input-file "day-2.input"))]
(let loop [(x 0) (y 0) (aim 0)]
(let [(amount (string->number (get-word file)))
(move (get-word file))]
(cond
((string=? move "forward")
(loop
(+ x amount)
(+ y (* aim amount))
aim))
((string=? move "down")
(loop x y (+ aim amount)))
((string=? move "up")
(loop x y (- aim amount)))
(else
(display "part 2: ")
(display (* x y))
(newline)
(close-port file))))))
(call-with-input-file
"day-2.input"
(lambda (file)
(let loop [(x 0) (y 0) (aim 0)]
(let [(amount (string->number (get-word file)))
(move (get-word file))]
(cond
((string=? move "forward")
(loop
(+ x amount)
(+ y (* aim amount))
aim))
((string=? move "down")
(loop x y (+ aim amount)))
((string=? move "up")
(loop x y (- aim amount)))
(else
(display "part 2: ")
(display (* x y))
(newline)))))))