-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.scm
80 lines (72 loc) · 1.91 KB
/
day02.scm
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(module day02
(include "utils.scm")
)
(define (all-to-int arr)
(if (null? arr)
'()
(if (string? arr)
(string->number arr)
(cons (string->number (car arr)) (all-to-int (cdr arr)))
)
)
)
(define (parse-spaces-and-numbers arr)
(if (null? arr)
'()
(cons (all-to-int (string-split (car arr) " ")) (parse-spaces-and-numbers (cdr arr)))
)
)
(define (min-max arr minimum maximum)
(if (null? arr)
(list minimum maximum)
(if (< (car arr) minimum)
(if (> (car arr) maximum)
(min-max (cdr arr) (car arr) (car arr))
(min-max (cdr arr) (car arr) maximum)
)
(if (> (car arr) maximum)
(min-max (cdr arr) minimum (car arr))
(min-max (cdr arr) minimum maximum)
)
)
)
)
(define (diff arr)
(abs (- (car arr) (cadr arr)))
)
(define (find-sum arr sum)
(if (null? arr)
sum
(+ (find-sum (cdr arr) sum) (+ sum (diff (min-max (car arr) 10000 0))))
)
)
(define (find-divisible-quotient orig-arr arr item pos len)
(if (eq? pos len)
#f
(if (eq? 0 (modulo item (cadr arr)))
(/ item (cadr arr))
(if (eq? 0 (modulo (cadr arr) item))
(/ (cadr arr) item)
(let ((res (find-divisible-quotient orig-arr (cdr arr) item (+ pos 1) len)))
(if (number? res)
res
(find-divisible-quotient (cdr orig-arr) (cdr orig-arr) (cadr orig-arr) 1 (- len 1))
)
)
)
)
)
)
(define (find-divisible-sum arr sum)
(if (null? arr)
sum
(+ (find-divisible-sum (cdr arr) sum) (+ sum (find-divisible-quotient (car arr) (car arr) (caar arr) 1 (length (car arr)))))
)
)
(define (day2 in)
(display-with-types (find-sum in 0))
(display " <- part 1\n")
(display-with-types (find-divisible-sum in 0))
(display " <- part 2\n")
)
(day2 (parse-spaces-and-numbers (string-split (read-string (open-input-file "day02in.txt")) "\n")))