-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.rkt
31 lines (27 loc) · 1.02 KB
/
day1.rkt
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
#lang racket
(define (input->list f)
(for/list ([l (in-lines f)])
(string->number l)))
;; Simple brute force solution which finds every pair of numbers in the list.
(call-with-input-file "day1_in.txt"
(lambda (f)
(let* ([v (list->vector (input->list f))]
[len (vector-length v)])
(for* ([i (in-range len)]
[j (in-range len)]
#:unless (or (< j i) (eqv? i j)))
(if (eqv? (+ (vector-ref v i) (vector-ref v j)) 2020)
(printf "~a\n" (* (vector-ref v i) (vector-ref v j)))
'())))))
(call-with-input-file "day1_in.txt"
(lambda (f)
(let* ([v (list->vector (input->list f))]
[len (vector-length v)])
(for* ([i (in-range len)]
[j (in-range len)]
[k (in-range len)]
#:unless (or (<= k j) (<= j i)))
(if (eqv? (+ (vector-ref v i) (vector-ref v j) (vector-ref v k)) 2020)
(printf "~a\n"
(* (vector-ref v i) (vector-ref v j) (vector-ref v k)))
'())))))