aoc2021/rkt/02.2.rkt
2021-12-03 02:20:27 +07:00

19 lines
529 B
Racket

#lang racket
(let-values ([(_ x y)
(with-input-from-file "../input/02"
(lambda ()
(for/fold ([aim 0]
[x 0]
[y 0])
([line (in-lines)])
(let ([s (string-split line)])
(let ([op (car s)]
[num (string->number (car (cdr s)))])
(match op
["up" (values (- aim num) x y)]
["down" (values (+ aim num) x y)]
["forward" (values aim (+ x num) (+ y (* aim num)))]))))))
]) (values (cons x y) (* x y)))