aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-01-27 19:53:51 +0800
committerMistivia <i@mistivia.com>2024-01-27 19:53:51 +0800
commite482c6c5dac4bc3af2cba85a3e71524c58b9afe4 (patch)
tree1541b7e81a8fda2e97c94133573c493e5a96a513
parent087a111b3417cbda03a3453b3d16dd4d1cf54a9e (diff)
solve advent of code 2023 06 part1
-rw-r--r--advent-of-code/2023/06/1.rkt32
-rw-r--r--advent-of-code/2023/06/input3
2 files changed, 35 insertions, 0 deletions
diff --git a/advent-of-code/2023/06/1.rkt b/advent-of-code/2023/06/1.rkt
new file mode 100644
index 0000000..ea0d013
--- /dev/null
+++ b/advent-of-code/2023/06/1.rkt
@@ -0,0 +1,32 @@
+#lang racket
+
+(define in (open-input-file "input"))
+
+(define (read-num-list)
+ (define line (read-line in))
+ (map
+ string->number
+ (string-split
+ (cadr (string-split (string-trim line) ":")))))
+
+(define times (read-num-list))
+(define distances (read-num-list))
+
+(define games (map cons times distances))
+
+(define (calc time hold-time)
+ (* hold-time (- time hold-time)))
+
+(define (solve time distance)
+ (define (loop counter hold-time)
+ (if (> hold-time time)
+ counter
+ (if (> (calc time hold-time) distance)
+ (loop (+ 1 counter) (+ 1 hold-time))
+ (loop counter (+ 1 hold-time)))))
+ (loop 0 0))
+
+(display (apply * (map
+ (lambda (x) (solve (car x) (cdr x)))
+ games)))
+(newline)
diff --git a/advent-of-code/2023/06/input b/advent-of-code/2023/06/input
new file mode 100644
index 0000000..41dd40b
--- /dev/null
+++ b/advent-of-code/2023/06/input
@@ -0,0 +1,3 @@
+Time: 59 70 78 78
+Distance: 430 1218 1213 1276
+