From 020de432cbba41142302e5e654f8251aa37aff60 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Wed, 23 Nov 2022 09:39:24 -0500 Subject: [PATCH] day1/2: Add some scheme solutions All in the name of learning some CHICKEN :) --- 01/sol.scm | 35 +++++++++++++++++++++++++++++++++++ 02/sol.scm | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 01/sol.scm create mode 100644 02/sol.scm diff --git a/01/sol.scm b/01/sol.scm new file mode 100644 index 0000000..94100b7 --- /dev/null +++ b/01/sol.scm @@ -0,0 +1,35 @@ +(import (chicken io)) + +(define (read-num port) + (let ((line (read-line port))) + (if (eof-object? line) + #f + (string->number line)))) + +(define (part-1 port) + (let loop ((total 0) (prev #f) (num (read-num port))) + (define (next) + (loop total num (read-num port))) + (cond ((not num) total) + ((not prev) (next)) + ((< prev num) (loop (add1 total) num (read-num port))) + (else (next))))) + +(define (winsum vect) + (apply + (vector->list vect))) + +(define (part-2 port) + (let loop ((i 0) (total 0) (window #(0 0 0)) (num (read-num port))) + (if (not num) + total + (let ((old-val (winsum window))) + (vector-set! window (modulo i 3) num) + (if (and (> i 2) (< old-val (winsum window))) + (loop (add1 i) (add1 total) window (read-num port)) + (loop (add1 i) total window (read-num port))))))) + +(call-with-input-file "input" + (lambda (p) (print (part-1 p)))) + +(call-with-input-file "input" + (lambda (p) (print (part-2 p)))) diff --git a/02/sol.scm b/02/sol.scm new file mode 100644 index 0000000..c4fb304 --- /dev/null +++ b/02/sol.scm @@ -0,0 +1,41 @@ +(import (chicken io) + (chicken string)) + +(define (part-1 port) + (let loop ((h 0) (d 0)) + (let ((l (read-line port))) + (if (eof-object? l) + (* h d) + (let* ((parts (string-split l)) + (act (car parts)) + (mag (string->number (cadr parts)))) + (cond ((equal? act "down") + (loop h (+ d mag))) + ((equal? act "up") + (loop h (- d mag))) + ((equal? act "forward") + (loop (+ h mag) d)) + (else (error "Bad action " act)))))))) + +(define (part-2 port) + (let loop ((h 0) (d 0) (a 0)) + (let ((l (read-line port))) + (if (eof-object? l) + (* h d) + (let* ((parts (string-split l)) + (act (car parts)) + (mag (string->number (cadr parts)))) + (cond ((equal? act "down") + (loop h d (+ a mag))) + ((equal? act "up") + (loop h d (- a mag))) + ((equal? act "forward") + (loop (+ h mag) (+ d (* a mag)) a)) + (else (error "Bad action " act)))))))) + + +(call-with-input-file "input" + (lambda (p) (print (part-1 p)))) + +(call-with-input-file "input" + (lambda (p) (print (part-2 p)))) -- libgit2 1.8.1