advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

commit c420ace3d85d29e8beebe6b58cd84b8f68757810
parent 7c2caea1b2df31b38e53c41606c550fffdc62774
Author: aabacchus <bvnfuller@gmail.com>
Date:   Fri,  3 Dec 2021 00:51:07 +0000

21.2.2

Diffstat:
M2021/02/2.c | 6++++--
M2021/02/2.scm | 8+++++---
2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/2021/02/2.c b/2021/02/2.c @@ -16,6 +16,7 @@ main(int argc, char **argv) { } int coords[2] = {0, 0}; + int aim = 0; char *buf = NULL; size_t buflen = 0; @@ -24,12 +25,13 @@ main(int argc, char **argv) { switch (buf[0]) { case 'f': /* forward */ coords[0] += param; + coords[1] += param * aim; break; case 'u': /* up */ - coords[1] -= param; + aim -= param; break; case 'd': /* down */ - coords[1] += param; + aim += param; break; } } diff --git a/2021/02/2.scm b/2021/02/2.scm @@ -4,11 +4,13 @@ (define txtlines (call-with-input-file "input" read-lines)) (define splitlines (map (lambda (s) (string-split s " ")) txtlines)) +(define aim 0) (define (getvec-line splitline) (cond - ((string=? (car splitline) "forward") (list (string->number (cadr splitline)) 0)) - ((string=? (car splitline) "down") (list 0 (string->number (cadr splitline)))) - ((string=? (car splitline) "up") (list 0 (* -1 (string->number (cadr splitline))))) + ((string=? (car splitline) "forward") + (let ((p (string->number (cadr splitline)))) (list p (* p aim)))) + ((string=? (car splitline) "down") (set! aim (+ aim (string->number (cadr splitline)))) (list 0 0)) + ((string=? (car splitline) "up") (set! aim (+ aim (* -1 (string->number (cadr splitline))))) (list 0 0)) (else (list 0 0)))) (define (move lines)