11/* eslint-env mocha */
22const { expect } = require ( 'chai' )
33const { validateRecords } = require ( './expenseValidation' )
4+
5+ /**
6+ * Sum all the values in an array
7+ */
8+ const arrSum = ( arr ) => arr . reduce ( ( x , y ) => x + y , 0 )
9+ /**
10+ * Multiply all the values in an array
11+ */
12+ const arrMult = ( arr ) => arr . reduce ( ( x , y ) => x * y , 1 )
413const testData = [
514 1721 ,
615 979 ,
@@ -15,26 +24,19 @@ describe('--- 2020 Day 1: Report Repair ---', () => {
1524 describe ( 'validateRecords()' , ( ) => {
1625 it ( 'it finds the two records that sum to 2020' , ( ) => {
1726 const expected = [ 1721 , 299 ]
18- const results = validateRecords ( testData , 2020 )
27+ const results = validateRecords ( testData , undefined , 2 )
1928 // Should be 2 results
2029 expect ( results . length ) . to . equal ( 2 )
2130 // Result order is unnecessary, but all expected hould be in the result set
2231 expected . forEach ( result => {
2332 expect ( testData . indexOf ( result ) ) . to . be . greaterThan ( - 1 )
2433 } )
25- } )
26- it ( 'it can find a specified number of records adding up to 2020' , ( ) => {
27- const expected = [ 979 , 366 , 675 ]
28- const results = validateRecords ( testData , undefined , 3 )
29- // Should same number of results
30- expect ( results . length ) . to . equal ( expected . length )
31- // Result order is unnecessary, but all expected hould be in the result set
32- expected . forEach ( result => {
33- expect ( testData . indexOf ( result ) ) . to . be . greaterThan ( - 1 )
34- } )
34+ // Results add up to the checksum
35+ expect ( arrSum ( results ) ) . to . equal ( 2020 )
36+ // Confirm the multiplied total
37+ expect ( arrMult ( results ) ) . to . equal ( 514579 )
3538 } )
3639 it ( 'it supports specifying an alternate checksum' , ( ) => {
37- const arrSum = ( arr ) => arr . reduce ( ( x , y ) => x + y , 0 )
3840 const expected = [ testData [ 3 ] , testData [ 5 ] ]
3941 const checksum = arrSum ( expected )
4042 const results = validateRecords ( testData , checksum )
@@ -55,4 +57,22 @@ describe('--- 2020 Day 1: Report Repair ---', () => {
5557 } )
5658 } )
5759 } )
60+ describe ( 'Part 2' , ( ) => {
61+ describe ( 'validateRecords()' , ( ) => {
62+ it ( 'it can find a specified number of records adding up to 2020' , ( ) => {
63+ const expected = [ 979 , 366 , 675 ]
64+ const results = validateRecords ( testData , undefined , 3 )
65+ // Should same number of results
66+ expect ( results . length ) . to . equal ( expected . length )
67+ // Result order is unnecessary, but all expected hould be in the result set
68+ expected . forEach ( result => {
69+ expect ( testData . indexOf ( result ) ) . to . be . greaterThan ( - 1 )
70+ } )
71+ // Results add up to the checksum
72+ expect ( arrSum ( results ) ) . to . equal ( 2020 )
73+ // Confirm the multiplied total
74+ expect ( arrMult ( results ) ) . to . equal ( 241861950 )
75+ } )
76+ } )
77+ } )
5878} )
0 commit comments