day 2
This commit is contained in:
1000
day-2.input
Normal file
1000
day-2.input
Normal file
File diff suppressed because it is too large
Load Diff
41
day-2.scm
Normal file
41
day-2.scm
Normal file
@@ -0,0 +1,41 @@
|
||||
(define (get-word text-port)
|
||||
(let loop [(w '())]
|
||||
(let [(c (get-char text-port))]
|
||||
(if (or (eof-object? c) (char-whitespace? c))
|
||||
(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))))))
|
||||
|
||||
(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))))))
|
||||
Reference in New Issue
Block a user