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

commit dd80dcc8133cd75b93a1118d113257abaef05536
parent 5136bae8753ccab874b0ecfc2954cd58e1e8c367
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sat,  2 Dec 2023 09:38:39 +0000

Add day 2 references and solution
Diffstat:
Aday02.scm | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aref/day02.txt | 5+++++
2 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/day02.scm b/day02.scm @@ -0,0 +1,97 @@ +; 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) + +(define spaces + (one-or-more (char-set #\space))) + +(define digit + (in char-set:digit)) + +(define digits + (as-string (one-or-more digit))) + +(define color + (any-of (char-seq "red") + (char-seq "green") + (char-seq "blue"))) + +(define dice-count + (sequence* ((count digits) + (_ (is #\space)) + (c color)) + (result (list c count)))) + +(define (rgb dice-count-list) + (map (lambda (key) (string->number (car (alist-ref key dice-count-list equal? '("0"))))) + '("red" "green" "blue"))) + +(define hand-shown + (sequence* ((first dice-count) + (rest (zero-or-more (preceded-by (char-seq ", ") dice-count)))) + (result (rgb (cons first rest))))) + +(define hands-shown + (sequence* ((first hand-shown) + (rest (zero-or-more (preceded-by (char-seq "; ") hand-shown)))) + (result (cons first rest)))) + +(define game-line + (sequence* ((_ (char-seq "Game ")) + (id digits) + (_ (char-seq ": ")) + (hands hands-shown) + (_ (is #\newline))) + (result (list id hands)))) + +(define all-data + (one-or-more game-line)) + +(define (hand-valid-1? hand) + (and (<= (car hand) 12) + (<= (cadr hand) 13) + (<= (caddr hand) 14))) + +(define (hands-valid-1? hands) + (or (null? hands) + (and (hand-valid-1? (car hands)) + (hands-valid-1? (cdr hands))))) + +(define (game-term-1 game) + (if (hands-valid-1? (cadr game)) + (string->number (car game)) + 0)) + +(define (answer-1 game-list) + (apply + (map game-term-1 game-list))) + +(define (rgb-max rgb-list cur-max) + (if (null? rgb-list) + cur-max + (rgb-max (cdr rgb-list) + (map (lambda (x) (apply max x)) (zip (car rgb-list) cur-max))))) + +(define (game-power game) + (apply * (rgb-max (cadr game) '(0 0 0)))) + +(define (answer-2 game-list) + (apply + (map game-power game-list))) + +(define data (parse all-data (read-string))) +(write-line (conc "First puzzle: " (answer-1 data))) +(write-line (conc "Second puzzle: " (answer-2 data))) diff --git a/ref/day02.txt b/ref/day02.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green