From c9bf66960a185c23cb38e22473ba01bfa05652d4 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Thu, 9 Dec 2021 20:04:48 +0100 Subject: [PATCH] add days 1 and 2 --- README.md | 18 +++++++++--------- day01.scm | 30 ++++++++++++++++++++++++++++++ day02.scm | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 9 deletions(-) create mode 100755 day01.scm create mode 100755 day02.scm diff --git a/README.md b/README.md index 79799fd..1ccc43b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # Advent of Code 2021 -| Day | Chicken Scheme | Scala | Python | Nim | -|-----|----------------|--------|--------|-----| -| 1 | ✅ (to commit) | | | | -| 2 | ✅ (to commit) | | | | -| 3 | part 1 | part 1 | ✅ | | -| 4 | | | ✅ | | -| 5 | | | ✅ | | -| 6 | | | | ✅ | -| 7 | ✅ | | | | +| Day | CHICKEN | Scala | Python | Nim | +|-----|---------|--------|--------|-----| +| 1 | ✅ | | | | +| 2 | ✅ | | | | +| 3 | part 1 | part 1 | ✅ | | +| 4 | | | ✅ | | +| 5 | | | ✅ | | +| 6 | | | | ✅ | +| 7 | ✅ | | | | diff --git a/day01.scm b/day01.scm new file mode 100755 index 0000000..81103b1 --- /dev/null +++ b/day01.scm @@ -0,0 +1,30 @@ +(import (chicken io)) +(import (chicken format)) +(import srfi-1) + +(define (parse inputs) + (let ((line (read-line))) + (if (not (eof-object? line)) + (parse (cons (string->number line) inputs)) + inputs))) + +(define (inc-if cond num) + (if cond (+ num 1) num)) + +(define (part1 inputs previous counter) + (if (not (null? inputs)) + (let ((head (car inputs))) + (part1 (cdr inputs) head (inc-if (<= head previous) counter))) + counter)) + + +(define (part2 inputs previous counter) + (if (not (eq? inputs '())) + (let ((sum (apply + (take-right! inputs 3)))) + (part2 (cdr inputs) sum (inc-if (<= sum previous) counter))) + counter)) + +(let ((inputs (parse '()))) + (format #t "inputs = ~A~%" inputs) + (format #t "part1 = ~A~%" (part1 inputs 0 0)) + (format #t "part2 = ~A~%" (part2 inputs 0 0))) diff --git a/day02.scm b/day02.scm new file mode 100755 index 0000000..108f15e --- /dev/null +++ b/day02.scm @@ -0,0 +1,48 @@ +(import (chicken io)) +(import (chicken format)) +(import (chicken string)) +(import (chicken condition)) +(import srfi-1) + +(define (parse-line line) + (let-values (((command offset) (apply values (string-split line)))) + (list command (string->number offset)))) + +(define (parse inputs) + (map parse-line (read-lines))) + +(define (part1-direction head tail h d) + (let-values (((command offset) (apply values head))) + (cond ((equal? command "forward") + (part1 tail (+ h offset) d)) + ((equal? command "down") + (part1 tail h (+ d offset))) + ((equal? command "up") + (part1 tail h (- d offset))) + (else (abort (format "did not expect command '~A'" command)))))) + +(define (part1 directions h d) + (if (not (null? directions)) + (part1-direction (car directions) (cdr directions) h d) + (* h d))) + + +(define (part2-direction head tail horiz depth aim) + (let-values (((command offset) (apply values head))) + (cond ((equal? (car command) "forward") + (part2 tail (+ horiz offset) (+ depth (* aim offset)) aim)) + ((equal? command "down") + (part2 tail horiz depth (+ aim offset))) + ((equal? command "up") + (part2 tail horiz depth (- aim offset))) + (else (abort (format "did not expect command '~A'" command)))))) + +(define (part2 directions horiz depth aim) + (if (not (null? directions)) + (part2-direction (car directions) (cdr directions) horiz depth aim) + (* horiz depth))) + + +(let ((directions (parse '()))) + (format #t "part1 = ~A~%" (part1 directions 0 0)) + (format #t "part2 = ~A~%" (part2 directions 0 0 0)))