File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,25 @@ class Plants {
122
122
} )
123
123
return output . trim ( )
124
124
}
125
+
126
+ /**
127
+ * Sums the number of plants present in all generations
128
+ */
129
+ getPlantTotal ( ) {
130
+ return this . generations . reduce ( ( gacc , g ) => {
131
+ return gacc + g . filter ( ( p ) => p . state === '#' ) . length
132
+ } , 0 )
133
+ }
134
+
135
+ /**
136
+ * Generates a checksum calculated by summing the positions of all pots containing plants
137
+ * in a specified generation
138
+ * @param {Number } generation to generate checksum for
139
+ */
140
+ getCheckSum ( generation ) {
141
+ return this . generations [ generation ] . filter ( ( p ) => p . state === '#' )
142
+ . reduce ( ( pacc , p ) => pacc + p . position , 0 )
143
+ }
125
144
}
126
145
127
146
module . exports = {
Original file line number Diff line number Diff line change @@ -153,5 +153,27 @@ describe('--- Day 12: Subterranean Sustainability ---', () => {
153
153
expect ( actual ) . to . equal ( expected )
154
154
} )
155
155
} )
156
+ describe ( 'getPlantTotal' , ( ) => {
157
+ it ( 'sums the total number of plants in all generations' , ( ) => {
158
+ const expected = 264
159
+ const plantTracker = new Plants ( initialState , rules )
160
+ for ( let gen = 1 ; gen <= 20 ; gen ++ ) {
161
+ plantTracker . advance ( )
162
+ }
163
+ const actual = plantTracker . getPlantTotal ( )
164
+ expect ( actual ) . to . equal ( expected )
165
+ } )
166
+ } )
167
+ describe ( 'getCheckSum(generation)' , ( ) => {
168
+ it ( 'generates a checksum tallied by summing the positions of all pots containing plants' , ( ) => {
169
+ const expected = 325
170
+ const plantTracker = new Plants ( initialState , rules )
171
+ for ( let gen = 1 ; gen <= 20 ; gen ++ ) {
172
+ plantTracker . advance ( )
173
+ }
174
+ const actual = plantTracker . getCheckSum ( 20 )
175
+ expect ( actual ) . to . equal ( expected )
176
+ } )
177
+ } )
156
178
} )
157
179
} )
You can’t perform that action at this time.
0 commit comments