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 {
122122 } )
123123 return output . trim ( )
124124 }
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+ }
125144}
126145
127146module . exports = {
Original file line number Diff line number Diff line change @@ -153,5 +153,27 @@ describe('--- Day 12: Subterranean Sustainability ---', () => {
153153 expect ( actual ) . to . equal ( expected )
154154 } )
155155 } )
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+ } )
156178 } )
157179} )
You can’t perform that action at this time.
0 commit comments