-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent07.scm
64 lines (52 loc) · 1.92 KB
/
advent07.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
#!/usr/bin/guile -s
!#
(use-modules ((ice-9 string-fun) #:select (string-replace-substring))
((srfi srfi-1) #:select (filter-map any))
((srfi srfi-64) #:select (test-begin
test-end
test-equal))
((f) #:select (read-lines))
((algorithms) #:select (sum)))
(define (input filename)
(read-lines filename))
(define (permutations n ops)
(if (= n 0)
'(())
(let ((sub-perms (permutations (- n 1) ops)))
(apply append
(map (λ (e) (map (λ (perm) (cons e perm)) sub-perms)) ops)))))
(define (perm-ok? perm total numbers)
(define (aux operations acc left)
(if (or (null? operations) (> acc total))
#f
(let* ([operation (car operations)]
[result (operation acc (car left))])
(if (and (null? (cdr operations)) (= total result))
#t
(aux (cdr operations) result (cdr left))))))
(aux perm (car numbers) (cdr numbers)))
(define (ok? line ops)
(let* ([total (car line)]
[numbers (cdr line)]
[perms (permutations (- (length numbers) 1) ops)])
(any (λ (perm) (perm-ok? perm total numbers)) perms)))
(define (|| a b)
(+ (* a (expt 10 (1+ (floor (log10 b))))) b))
(define (solve ops filename)
(let* ([data (map (λ (line) (map string->number (string-split (string-replace-substring line ":" "") #\space))) (input filename))])
(sum (filter-map (λ (line) (and (ok? line ops) (car line))) data))))
(define (part-1 filename)
(solve (list + *) filename))
(define (part-2 filename)
(solve (list + * ||) filename))
;; Part 1
(display (part-1 "day07.in"))
(newline)
;; Part 2
(display (part-2 "day07.in"))
(newline)
;; Tests
(test-begin "example")
(test-equal "Test part 1" 3749 (part-1 "day07-example.in"))
(test-equal "Test part 2" 11387 (part-2 "day07-example.in"))
(test-end "example")