@@ -5,17 +5,27 @@ def self.go
5
5
puts "Part 2: #{ day . part2 } "
6
6
end
7
7
8
+ def initialize ( input = nil )
9
+ @input = input || real_input
10
+ end
11
+
12
+ # @example
13
+ # new(EXAMPLE_INPUT).part1 #=> 198
14
+ #
8
15
def part1
9
- diag = SubDiagnostics . new ( report )
16
+ diag = SubDiagnostics . new ( @input )
10
17
diag . power_consumption
11
18
end
12
19
20
+ # @example
21
+ # new(EXAMPLE_INPUT).part2 #=> 230
22
+ #
13
23
def part2
14
- diag = SubDiagnostics . new ( report )
24
+ diag = SubDiagnostics . new ( @input )
15
25
diag . life_support_rating
16
26
end
17
27
18
- def report
28
+ def real_input
19
29
File . read ( '../inputs/day03-input.txt' )
20
30
end
21
31
@@ -48,15 +58,27 @@ def initialize(report='')
48
58
end
49
59
end
50
60
61
+ # @example
62
+ # SubDiagnostics
63
+ # .new(Day03::EXAMPLE_INPUT)
64
+ # .power_consumption #=> 198
51
65
def power_consumption
52
66
gamma_rate * epsilon_rate
53
67
end
54
68
69
+ # @example
70
+ # SubDiagnostics
71
+ # .new(Day03::EXAMPLE_INPUT)
72
+ # .gamma_rate #=> 22
55
73
def gamma_rate
56
74
popular_bits
57
75
. to_i ( 2 )
58
76
end
59
77
78
+ # @example
79
+ # SubDiagnostics
80
+ # .new(Day03::EXAMPLE_INPUT)
81
+ # .epsilon_rate #=> 9
60
82
def epsilon_rate
61
83
popular_bits
62
84
. tr ( '01' , '10' )
@@ -74,14 +96,26 @@ def popular_bits
74
96
. join ( "" )
75
97
end
76
98
99
+ # @example
100
+ # SubDiagnostics
101
+ # .new(Day03::EXAMPLE_INPUT)
102
+ # .life_support_rating #=> 230
77
103
def life_support_rating
78
104
oxygen_generator_rating * co2_scrubber_rating
79
105
end
80
106
107
+ # @example
108
+ # SubDiagnostics
109
+ # .new(Day03::EXAMPLE_INPUT)
110
+ # .oxygen_generator_rating #=> 23
81
111
def oxygen_generator_rating
82
112
rating_filter ( method ( :most_common_bit_at ) )
83
113
end
84
114
115
+ # @example
116
+ # SubDiagnostics
117
+ # .new(Day03::EXAMPLE_INPUT)
118
+ # .co2_scrubber_rating #=> 10
85
119
def co2_scrubber_rating
86
120
rating_filter ( method ( :least_common_bit_at ) )
87
121
end
@@ -121,42 +155,3 @@ def bit_tally(input)
121
155
. map { |position | position . tally }
122
156
end
123
157
end
124
-
125
- require 'minitest'
126
-
127
- class TestDay03 < Minitest ::Test
128
-
129
- def setup
130
- @diag = SubDiagnostics . new ( Day03 ::EXAMPLE_INPUT )
131
- end
132
-
133
- def test_part1_gamma_rate
134
- assert_equal 22 , @diag . gamma_rate
135
- end
136
-
137
- def test_part1_epsilon_rate
138
- assert_equal 9 , @diag . epsilon_rate
139
- end
140
-
141
- def test_part1_power_consumption
142
- assert_equal 198 , @diag . power_consumption
143
- end
144
-
145
- def test_part2_oxygen_generator_rating
146
- assert_equal 23 , @diag . oxygen_generator_rating
147
- end
148
-
149
- def test_part2_co2_scrubber_rating
150
- assert_equal 10 , @diag . co2_scrubber_rating
151
- end
152
-
153
- def test_part2_life_support_rating
154
- assert_equal 230 , @diag . life_support_rating
155
- end
156
- end
157
-
158
- if ENV . key? 'TEST'
159
- require 'minitest/autorun'
160
- else
161
- Day03 . go
162
- end
0 commit comments