-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.rb
48 lines (37 loc) · 1.04 KB
/
day19.rb
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
# input parsing
input = IO.read("inputs/19").split("\n\n")
patterns = input[0].split(", ")
desired = input[1].split("\n")
# part 1
$solved = {}
def possible(s, patterns)
return true if $solved[s] or s.empty?
return false if $solved[s] == false
pos = patterns.map(&:size).uniq.map { |l|
suffixes = patterns.filter { |p| p.size == l }
sub = s[0..-l-1]
suffixes.map { |suf| sub + suf }.any? { |str| str == s } and possible(sub, patterns)
}.any?(true)
$solved[s] = pos
return pos
end
p desired.map { |s| possible(s, patterns) ? 1 : 0 }.inject(:+)
# part 2
$solved = {"" => 1}
def count(s, patterns)
return $solved[s] if $solved.include?(s)
sum = patterns.map(&:size).uniq.map { |l|
suffixes = patterns.filter { |p| p.size == l }
sub = s[0..-l-1]
prev = count(sub, patterns)
curr = suffixes.map { |suf| sub + suf }.count { |str| s == str }
if prev > 0 and curr > 0
prev * curr
else
0
end
}.inject(:+)
$solved[s] = sum
return sum
end
p desired.map { |s| count(s, patterns) }.inject(:+)