From 3955e3552091f609a3242f12c08a6d0a232bd402 Mon Sep 17 00:00:00 2001 From: chayleaf Date: Fri, 3 Dec 2021 02:20:27 +0700 Subject: [PATCH] day 2 racket --- rkt/02.1.rkt | 17 +++++++++++++++++ rkt/02.2.rkt | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 rkt/02.1.rkt create mode 100644 rkt/02.2.rkt diff --git a/rkt/02.1.rkt b/rkt/02.1.rkt new file mode 100644 index 0000000..4318d1a --- /dev/null +++ b/rkt/02.1.rkt @@ -0,0 +1,17 @@ +#lang racket + +(let-values ([(x y) + (with-input-from-file "../input/02" + (lambda () + (for/fold ([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 x (- y num))] + ["down" (values x (+ y num))] + ["forward" (values (+ x num) y)])))))) +]) (values (cons x y) (* x y))) + diff --git a/rkt/02.2.rkt b/rkt/02.2.rkt new file mode 100644 index 0000000..cf94372 --- /dev/null +++ b/rkt/02.2.rkt @@ -0,0 +1,18 @@ +#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))) +