aboutsummaryrefslogtreecommitdiff
path: root/06
diff options
context:
space:
mode:
Diffstat (limited to '06')
-rw-r--r--06/1.rkt32
-rw-r--r--06/2.rkt29
2 files changed, 61 insertions, 0 deletions
diff --git a/06/1.rkt b/06/1.rkt
new file mode 100644
index 0000000..5496476
--- /dev/null
+++ b/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
+ (λ (x) (solve (car x) (cdr x)))
+ games)))
+(newline)
diff --git a/06/2.rkt b/06/2.rkt
new file mode 100644
index 0000000..2d666ed
--- /dev/null
+++ b/06/2.rkt
@@ -0,0 +1,29 @@
+#lang racket
+
+(define in (open-input-file "input"))
+
+(define (read-num)
+ (define line (read-line in))
+ (string->number
+ (apply
+ string-append
+ (string-split
+ (cadr (string-split (string-trim line) ":"))))))
+
+(define time (read-num))
+(define distance (read-num))
+
+(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 (solve time distance))
+(newline)