1
1
/* eslint-env mocha */
2
2
const { expect } = require ( 'chai' )
3
3
const { 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 )
4
13
const testData = [
5
14
1721 ,
6
15
979 ,
@@ -15,26 +24,19 @@ describe('--- 2020 Day 1: Report Repair ---', () => {
15
24
describe ( 'validateRecords()' , ( ) => {
16
25
it ( 'it finds the two records that sum to 2020' , ( ) => {
17
26
const expected = [ 1721 , 299 ]
18
- const results = validateRecords ( testData , 2020 )
27
+ const results = validateRecords ( testData , undefined , 2 )
19
28
// Should be 2 results
20
29
expect ( results . length ) . to . equal ( 2 )
21
30
// Result order is unnecessary, but all expected hould be in the result set
22
31
expected . forEach ( result => {
23
32
expect ( testData . indexOf ( result ) ) . to . be . greaterThan ( - 1 )
24
33
} )
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 )
35
38
} )
36
39
it ( 'it supports specifying an alternate checksum' , ( ) => {
37
- const arrSum = ( arr ) => arr . reduce ( ( x , y ) => x + y , 0 )
38
40
const expected = [ testData [ 3 ] , testData [ 5 ] ]
39
41
const checksum = arrSum ( expected )
40
42
const results = validateRecords ( testData , checksum )
@@ -55,4 +57,22 @@ describe('--- 2020 Day 1: Report Repair ---', () => {
55
57
} )
56
58
} )
57
59
} )
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
+ } )
58
78
} )
0 commit comments