commit 4ee8c311b651ea5f03f881176f91cf4baa8d2576
parent affd5cc1889d6920eac7544e16b23530e8104c33
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Wed, 6 Dec 2023 20:21:46 -0500
Add 2023 day 06
Diffstat:
2 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/2023/day06/day06.scm b/2023/day06/day06.scm
@@ -0,0 +1,51 @@
+(use-modules (ice-9 popen))
+(use-modules (ice-9 rdelim))
+(use-modules (ice-9 format))
+(use-modules (ice-9 receive))
+(use-modules (srfi srfi-1))
+
+(define (parse-numbers numbers)
+ (filter (lambda (x) x) (map string->number (string-split numbers #\ ))))
+
+(define (parse-input filename)
+ (let ((file (open-input-file filename))
+ (time '())
+ (distance '()))
+ (let ((line (car (%read-line file))))
+ (set! time (parse-numbers (car (cdr (string-split line #\:))))))
+ (let ((line (car (%read-line file))))
+ (set! distance (parse-numbers (car (cdr (string-split line #\:))))))
+ (map cons time distance)))
+
+(define (solve-quadratic a b c)
+ (let ((tmp (sqrt (- (expt b 2) (* 4 a c)))))
+ (cons (/ (+ (- b) tmp) (* 2 a)) (/ (- (- b) tmp) (* 2 a)))))
+
+(define (count-record-beater time dist)
+ (let ((res (solve-quadratic -1 time (- dist))))
+ (inexact->exact (1+ (- (ceiling (1- (cdr res))) (floor (1+ (car res))))))))
+
+(let* ((input (parse-input "input.txt"))
+ (res (fold * 1 (map (lambda (x) (count-record-beater (car x) (cdr x))) input))))
+ ;; 4811940
+ (format #t "Part 1: ~d" res)
+ (newline))
+
+(define (parse-numbers-2 numbers)
+ (string->number (list->string (filter char-numeric? (string->list numbers)))))
+
+(define (parse-input-2 filename)
+ (let ((file (open-input-file filename))
+ (time '())
+ (distance '()))
+ (let ((line (car (%read-line file))))
+ (set! time (parse-numbers-2 (car (cdr (string-split line #\:))))))
+ (let ((line (car (%read-line file))))
+ (set! distance (parse-numbers-2 (car (cdr (string-split line #\:))))))
+ (cons time distance)))
+
+(let* ((input (parse-input-2 "input.txt"))
+ (res (count-record-beater (car input) (cdr input))))
+ ;; 30077773
+ (format #t "Part 2: ~d" res)
+ (newline))
diff --git a/2023/day06/input.txt b/2023/day06/input.txt
@@ -0,0 +1,2 @@
+Time: 41 96 88 94
+Distance: 214 1789 1127 1055