summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlamp2023-12-06 20:14:46 +0000
committerlamp2023-12-06 20:14:46 +0000
commitde3954129bfa3a746a313181cae71e1f384dda59 (patch)
treeb6fced528c73c2d82a95b8bc673d150630097923
parent92d487dc052788b0ef77071cccad2427c20f66df (diff)
add day 6 solutions
-rwxr-xr-x6-2.scm30
-rwxr-xr-x6.scm33
-rw-r--r--6.txt2
-rw-r--r--6s.txt2
4 files changed, 67 insertions, 0 deletions
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)))))
diff --git a/6.scm b/6.scm
new file mode 100755
index 0000000..9a440c4
--- /dev/null
+++ b/6.scm
@@ -0,0 +1,33 @@
+#!/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 (split-up nums)
+ (map string->number (filter (lambda (x) (not (string-null? x))) (string-split nums char-set:whitespace))))
+
+(define (distance total-time hold-time)
+ (* (- total-time hold-time) hold-time))
+
+(define (all-distances time)
+ (let ((distances '()))
+ (do ((i 0 (1+ i))) ((> i time))
+ (set! distances (append distances `(,(distance time i)))))
+ distances))
+
+(let* ((port (open-input-file "6.txt"))
+ (lines (read-lines port))
+ (races (map (lambda (x y) (list x y))
+ (split-up (match:substring (string-match ": ([0-9| ]+)" (car lines)) 1))
+ (split-up (match:substring (string-match ": ([0-9| ]+)" (cadr lines)) 1))))
+ )
+ (close-port port)
+ (display (fold * 1 (map (lambda (x) (length (filter (lambda (y) (> y (cadr x))) (all-distances (car x))))) races))))
diff --git a/6.txt b/6.txt
new file mode 100644
index 0000000..078c0bb
--- /dev/null
+++ b/6.txt
@@ -0,0 +1,2 @@
+Time: 56 71 79 99
+Distance: 334 1135 1350 2430
diff --git a/6s.txt b/6s.txt
new file mode 100644
index 0000000..fb025ef
--- /dev/null
+++ b/6s.txt
@@ -0,0 +1,2 @@
+Time: 7
+Distance: 9