aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/06/1.rkt
blob: ea0d013d770de377c0b38e0d9fafda5895a6889f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)