commit 4c97f41a6e21cb9fa1c1d9aa0ad1f2e25bb8ad20
parent cdd5731e0753015c0ba26ca215f903bbe9708495
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Sat, 9 Dec 2023 09:14:05 +0000
Add day 9 reference and solution
Diffstat:
2 files changed, 98 insertions(+), 0 deletions(-)
diff --git a/2023/day09.scm b/2023/day09.scm
@@ -0,0 +1,95 @@
+; Copyright (c) 2023, Natacha Porté
+;
+; Permission to use, copy, modify, and distribute this software for any
+; purpose with or without fee is hereby granted, provided that the above
+; copyright notice and this permission notice appear in all copies.
+;
+; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+(import (chicken io) (chicken string)
+ comparse
+ srfi-1
+ srfi-14)
+
+;;;;;;;;;;;;;;;;;
+;; Input parsing
+
+(define (as-number parser)
+ (bind (as-string parser)
+ (lambda (s)
+ (result (string->number s)))))
+
+(define spaces
+ (zero-or-more (is #\space)))
+
+(define digit
+ (in char-set:digit))
+
+(define digits
+ (as-number (one-or-more digit)))
+
+(define num
+ (sequence* ((sign (maybe (is #\-)))
+ (n digits))
+ (result (if sign (- n) n))))
+
+(define number-list
+ (sequence* ((data (zero-or-more (preceded-by spaces num)))
+ (_ (is #\newline)))
+ (result data)))
+
+(define all-data
+ (one-or-more number-list))
+
+(define data (parse all-data (read-string)))
+(write-line (conc "Input: " data))
+
+;;;;;;;;;;;;;;;;;
+;; First Puzzle
+
+(define (differentiate l)
+ (let loop ((prev (car l)) (rest (cdr l)) (acc '()))
+ (if (null? rest)
+ (reverse acc)
+ (loop (car rest) (cdr rest) (cons (- (car rest) prev) acc)))))
+
+(define (differentiate* l)
+ (let ((result (differentiate l)))
+ (write-line (conc "differentiate " l " -> " result))
+ result))
+
+(define (integrate first l)
+ (let loop ((prev first) (rest l) (acc (list first)))
+ (if (null? rest)
+ (reverse acc)
+ (let ((new-term (+ (car rest) prev)))
+ (loop new-term (cdr rest) (cons new-term acc))))))
+
+(define (integrate* first l)
+ (let ((result (integrate first l)))
+ (write-line (conc "integrate " first " " l " -> " result))
+ result))
+
+(define (process-1 l)
+ (if (apply = l)
+ (cons (car l) l)
+ (integrate (car l) (process-1 (differentiate l)))))
+
+(write-line (conc "First puzzle: "
+ (apply + (map (lambda (l) (last (process-1 l))) data))))
+
+;;;;;;;;;;;;;;;;;
+;; Second Puzzle
+
+(define (process-2 l)
+ (if (apply = l)
+ (car l)
+ (- (car l) (process-2 (differentiate l)))))
+
+(write-line (conc "Second puzzle: " (apply + (map process-2 data))))
diff --git a/2023/ref/day09.txt b/2023/ref/day09.txt
@@ -0,0 +1,3 @@
+0 3 6 9 12 15
+1 3 6 10 15 21
+10 13 16 21 30 45