aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 020de432cbba41142302e5e654f8251aa37aff60 (patch)
parent 6a646f375e801d789c4c402aa128194012785f0b
Author: Alex Karle <alex@alexkarle.com>
Date:   Wed, 23 Nov 2022 09:39:24 -0500

day1/2: Add some scheme solutions

All in the name of learning some CHICKEN :)

Diffstat:
A01/sol.scm | 35+++++++++++++++++++++++++++++++++++
A02/sol.scm | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/01/sol.scm 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 @@ -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))))