-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_a.rb
62 lines (50 loc) · 1.2 KB
/
3_a.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
50
51
52
53
54
55
56
57
58
59
60
61
62
# typed: true
# frozen_string_literal: true
class Level
class << self
def run(input)
status = Status.from_raw_diagnostics(input)
status.analyze!
puts "Power Consumption: #{status.power_consumption}"
end
end
end
class Status
extend T::Sig
sig { returns(T::Array[T::Array[String]]) }
attr_accessor :diagnostics
sig { returns(Integer) }
attr_accessor :most_common_bits
sig { returns(Integer) }
attr_accessor :mask
class << self
extend T::Sig
sig { params(data_string: String).returns(Status) }
def from_raw_diagnostics(data_string)
Status.new(data_string.split("\n").map(&:chars))
end
end
sig { params(diagnostics: T::Array[T::Array[String]]).void }
def initialize(diagnostics)
@diagnostics = diagnostics
end
sig { void }
def analyze!
set_mask!
@most_common_bits =
diagnostics
.transpose
.map { |pos| pos.count('1') >= diagnostics.length / 2.0 ? '1' : '0' }
.join
.to_i(2)
end
sig { returns(Integer) }
def power_consumption
most_common_bits * (most_common_bits ^ mask)
end
private
sig { void }
def set_mask!
@mask = ('1' * T.must(diagnostics.first).length).to_i(2)
end
end