-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.rb
40 lines (31 loc) · 957 Bytes
/
solution.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
# frozen_string_literal: true
require_relative '../solution'
module Day06
class Solution < Solution
def solve_part01
input_raw.split(/\n\n/).map { |g| PassengerGroup.new_from(g).yes_answers.size }.sum
end
def solve_part02
input_raw.split(/\n\n/).map do |g|
pg = PassengerGroup.new_from(g.delete_suffix("\n"))
pg.yes_answers.select { |_, v| v >= pg.passenger_count }.size
end.sum
end
end
class PassengerGroup
attr_reader :passenger_count, :yes_answers
def initialize(passenger_count, yes_answers)
@passenger_count = passenger_count
@yes_answers = yes_answers
end
def self.new_from(str)
passenger_count = 1
yes_answers = {}
str.each_char do |char|
next passenger_count += 1 if char == "\n"
yes_answers.merge!(char => 1) { |_, old_val, new_val| old_val + new_val }
end
new(passenger_count, yes_answers)
end
end
end