Created
December 2, 2021 22:14
-
-
Save aaronjeline/b42c8a9505d7583e21dd131084c4c85a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang racket | |
| (require threading) | |
| (define (parse-line line) | |
| (match (string-split line " ") | |
| [(list dir amnt) | |
| (cons (parse-dir dir) (parse-amnt amnt))])) | |
| (define (parse-dir dir) | |
| (cond | |
| [(string=? "forward" dir) forward!] | |
| [(string=? "down" dir) down!] | |
| [(string=? "up" dir) up!])) | |
| (define (parse-amnt amnt) | |
| (string->number amnt)) | |
| (define position 0) | |
| (define depth 0) | |
| (define aim 0) | |
| (define (forward! n) | |
| (set! position (+ position n)) | |
| (set! depth (+ depth (* aim n)))) | |
| (define (up! n) | |
| (set! aim (- aim n))) | |
| (define (down! n) | |
| (set! aim (+ aim n))) | |
| (define src | |
| (~> | |
| (with-input-from-file | |
| "/tmp/input" | |
| (λ () (port->lines (current-input-port)))) | |
| (map parse-line _))) | |
| (for [(op (in-list src))] | |
| ((car op) (cdr op))) | |
| (displayln (format "Position: ~a" position)) | |
| (displayln (format "Depth: ~a" depth)) | |
| (displayln (format "Result: ~a" (* depth position))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment