From de3954129bfa3a746a313181cae71e1f384dda59 Mon Sep 17 00:00:00 2001 From: lamp Date: Wed, 6 Dec 2023 20:14:46 +0000 Subject: add day 6 solutions --- 6-2.scm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 6-2.scm (limited to '6-2.scm') diff --git a/6-2.scm b/6-2.scm new file mode 100755 index 0000000..71279a7 --- /dev/null +++ b/6-2.scm @@ -0,0 +1,30 @@ +#!/usr/bin/env -S guile -s +!# +(use-modules (ice-9 regex) + (ice-9 textual-ports) + (srfi srfi-1)) + +(define (read-lines port) + (letrec ((loop (lambda (l ls) + (if (eof-object? l) + ls + (loop (get-line port) (cons l ls)))))) + (reverse (loop (get-line port) '())))) + +(define (value-for-line line) + (string->number (string-filter char-set:digit line))) + +(define (travel-distance total-time hold-time) + (* (- total-time hold-time) hold-time)) + +(let* ((port (open-input-file "6.txt")) + (lines (read-lines port)) + (time (value-for-line (car lines))) + (distance (value-for-line (cadr lines))) + (current 0) + (count 0)) + (close-port port) + (while (<= current distance) + (set! current (travel-distance time count)) + (set! count (1+ count))) + (display (- (+ time 1) (* 2 (- count 1))))) -- cgit v1.2.3