aoc-2023

My solutions in CHICKEN scheme to Advent of Code 2023
git clone https://git.instinctive.eu/aoc-2023.git
Log | Files | Refs | README | LICENSE

day09.scm (2675B)


      1 ; Copyright (c) 2023, Natacha Porté
      2 ;
      3 ; Permission to use, copy, modify, and distribute this software for any
      4 ; purpose with or without fee is hereby granted, provided that the above
      5 ; copyright notice and this permission notice appear in all copies.
      6 ;
      7 ; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8 ; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9 ; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     10 ; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11 ; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     12 ; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     13 ; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     14 
     15 (import (chicken io) (chicken string)
     16         comparse
     17         srfi-1
     18         srfi-14)
     19 
     20 ;;;;;;;;;;;;;;;;;
     21 ;; Input parsing
     22 
     23 (define (as-number parser)
     24   (bind (as-string parser)
     25         (lambda (s)
     26           (result (string->number s)))))
     27 
     28 (define spaces
     29   (zero-or-more (is #\space)))
     30 
     31 (define digit
     32   (in char-set:digit))
     33 
     34 (define digits
     35   (as-number (one-or-more digit)))
     36 
     37 (define num
     38   (sequence* ((sign (maybe (is #\-)))
     39               (n digits))
     40     (result (if sign (- n) n))))
     41 
     42 (define number-list
     43   (sequence* ((data (zero-or-more (preceded-by spaces num)))
     44               (_ (is #\newline)))
     45     (result data)))
     46 
     47 (define all-data
     48   (one-or-more number-list))
     49 
     50 (define data (parse all-data (read-string)))
     51 (write-line (conc "Input: " data))
     52 
     53 ;;;;;;;;;;;;;;;;;
     54 ;; First Puzzle
     55 
     56 (define (differentiate l)
     57   (let loop ((prev (car l)) (rest (cdr l)) (acc '()))
     58     (if (null? rest)
     59         (reverse acc)
     60         (loop (car rest) (cdr rest) (cons (- (car rest) prev) acc)))))
     61 
     62 (define (differentiate* l)
     63   (let ((result (differentiate l)))
     64     (write-line (conc "differentiate " l " -> " result))
     65     result))
     66 
     67 (define (integrate first l)
     68   (let loop ((prev first) (rest l) (acc (list first)))
     69     (if (null? rest)
     70         (reverse acc)
     71         (let ((new-term (+ (car rest) prev)))
     72           (loop new-term (cdr rest) (cons new-term acc))))))
     73 
     74 (define (integrate* first l)
     75   (let ((result (integrate first l)))
     76     (write-line (conc "integrate " first " " l " -> " result))
     77     result))
     78 
     79 (define (process-1 l)
     80   (if (apply = l)
     81       (cons (car l) l)
     82       (integrate (car l) (process-1 (differentiate l)))))
     83 
     84 (write-line (conc "First puzzle:  "
     85   (apply + (map (lambda (l) (last (process-1 l))) data))))
     86 
     87 ;;;;;;;;;;;;;;;;;
     88 ;; Second Puzzle
     89 
     90 (define (process-2 l)
     91   (if (apply = l)
     92       (car l)
     93       (- (car l) (process-2 (differentiate l)))))
     94 
     95 (write-line (conc "Second puzzle: " (apply + (map process-2 data))))