Skip to content

Commit d4743bb

Browse files
authored
Merge pull request lambda-study-group#4 from jvrn3/main
day 1
2 parents bcebb21 + 83ad64a commit d4743bb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2020/jvrn3/racket/day1.rkt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#lang racket
2+
3+
; receives a function and a list of integers
4+
; the function f determines if it's gonna be a sum of two or three numbers
5+
(define (find-2020 f xs)
6+
(let ([value (f (first xs) (rest xs))])
7+
(if (number? value) value (find-2020 f (rest xs)))))
8+
9+
; returns the multiplication of two numbers which the sum is equal to 2020
10+
; if there is no such a pair, returns false
11+
(define (check-two x xs)
12+
(cond
13+
[(null? xs) #f]
14+
[(equal? (+ x (first xs)) 2020)
15+
(* x (first xs))]
16+
[else
17+
(check-two x (rest xs))]))
18+
19+
20+
; same as check-two, but for three numbers
21+
(define (check-three x xs)
22+
(cond
23+
[(null? xs) #f]
24+
[else
25+
(let ([value (compare-three x (first xs) xs)])
26+
(if (number? value) value (check-three x (rest xs))))]))
27+
28+
29+
; if there's some number from xs that x + y + number is equal to 2020, returns their multiplication
30+
; otherwise returns false
31+
(define (compare-three x y xs)
32+
(cond
33+
[(null? xs) #f]
34+
[(equal? (+ x y (first xs)) 2020)
35+
(* x y (first xs))]
36+
[else (compare-three x y (rest xs))]))
37+
38+
39+

0 commit comments

Comments
 (0)