-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.rb
49 lines (43 loc) · 1.04 KB
/
solve.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
49
require 'set'
class Solve
def self.run
File.open('./input') do |f|
input = f.read
puts "Part one: sum of disjunct counts: #{sum_of_disjunct_counts(input)}"
puts "Part two: sum of conjunct counts: #{sum_of_conjunct_counts(input)}"
end
end
def self.sum_of_conjunct_counts(input)
group(input)
.map { |answers| answer_intersection(answers) }
.map { |set| set.size }
.reduce(&:+)
end
def self.answer_intersection(answers)
first_seen = answers.first.chars.to_set
answers.reduce(first_seen) do |already_seen, answer|
already_seen.intersection(answer.chars.to_set)
end
end
def self.sum_of_disjunct_counts(input)
group(input)
.map(&:join)
.map { |group| group.chars.to_set.size }
.reduce(:+)
end
def self.group(input)
input.lines
.map(&:chomp)
.reduce([[]]) do |accum, ansr|
if ansr.empty?
accum << []
else
accum.last << ansr
end
accum
end
end
end
if __FILE__ == $0
Solve.run
end