-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.el
More file actions
52 lines (44 loc) · 1.23 KB
/
19.el
File metadata and controls
52 lines (44 loc) · 1.23 KB
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
(require 'dash)
(require 's)
(defun read-input (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(buffer-string)))
(defun parse-input (input-string)
(-let (((towels-str logos-str) (split-string input-string "\n\n" t) towels logos))
(setq towels (-> towels-str (s-trim) (string-split ", " t)))
(setq logos (-> logos-str (s-trim) (s-lines)))
(list towels logos)))
(defun logo-ways (towels logo)
(-let [dp (make-vector (1+ (length logo)) 0)]
(setf (aref dp 0) 1)
(dolist (i (-iota (length logo) 1))
(dolist (towel towels)
(-let [l (length towel)]
(when (and (<= l i)
(equal towel (substring logo (- i l) i)))
(cl-incf (aref dp i) (aref dp (- i l)))))))
(aref dp (length logo))))
(defun part1 (input)
(-let [(towels logos) input]
(->> logos
(-reject (-compose #'zerop (-partial #'logo-ways towels)))
(length))))
(defun part2 (input)
(-let [(towels logos) input]
(->> logos
(-map (-partial #'logo-ways towels))
(-sum))))
(setq example (parse-input "
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb"))
(setq input (parse-input (read-input "19.txt")))
(part1 input)
(part2 input)