-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04p1.arc
64 lines (54 loc) · 1.51 KB
/
day04p1.arc
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
(= winning-card nil)
(= winning-seq nil)
(= cards nil)
(= random-numbers nil)
(= drawn-numbers nil)
(def run-puzzle (e)
(split-data (puzzle-input 04 'chrs e))
(play-bingo))
#|
Take the input, and extract a list of random numbers `random-numbers`
and a list of cards `cards`.
|#
(def split-data (data)
(let parts (tokens (subst #\! "\n\n" data) #\!) ; split on double newlines
(= random-numbers (readall (subst #\ "," (car parts))))
(= cards (map
[map readall (tokens _ #\newline)]
(cdr parts)))))
#|
True if all items in sequence have been drawn, nil otherwise.
|#
(def is-winning-seq (seq)
(is
(count [pos _ drawn-numbers] seq) ; amount of numbers in this row that has been drawn
(len seq)))
(def has-winning-row (card)
(keep is-winning-seq card))
(def rotate-matrix (matrix)
(accum outer
(forlen i (car matrix)
(outer (accum inner
(forlen j matrix
(inner ((matrix j) i))))))))
(def has-winning-col (card)
(keep is-winning-seq (rotate-matrix card)))
(def is-winner (card)
(or
(has-winning-row card)
(has-winning-col card)))
#|
Move first number in `random-numbers` to `drawn-numbers`.
|#
(def draw-number ()
(push (pop random-numbers) drawn-numbers))
(def calculate-score (card)
(* (sum int (rem [pos _ drawn-numbers] (flat winning-card))) (car drawn-numbers)))
(def play-bingo ()
(while (is winning-card nil)
(draw-number)
(each card cards
(awhen (is-winner card)
(= winning-card card)
(= winning-seq it))))
(calculate-score winning-card))