Skip to content

Commit e15993a

Browse files
feat[2020-day-01]: find 3 expenses totaling 2020
1 parent 6aad8ca commit e15993a

File tree

2 files changed

+40
-49
lines changed

2 files changed

+40
-49
lines changed

2020/day-01/expenseValidation.test.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
33
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)
413
const 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
})

2020/day-01/solution.js

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,18 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
1717

1818
const part1 = () => {
1919
const data = resetInput()
20-
const results = validateRecords(data)
21-
22-
return results[0] * results[1]
20+
return validateRecords(data) // Find 2 results for 2020
21+
.reduce((total, res) => total * res, 1)
2322
}
2423

25-
// const part2 = ({ target, maxNoun, maxVerb }) => {
26-
// // Helper for running the program with specified noun and verb inputs
27-
// const tryProgram = ({
28-
// noun,
29-
// verb
30-
// }) => {
31-
// const data = resetInput()
32-
// data[1] = noun
33-
// data[2] = verb
34-
// runProgram({ data })
35-
// console.debug(`Running with noun:${noun} and verb:${verb} produces ${data[0]}`)
36-
// return Number(data[0])
37-
// }
38-
39-
// // Manipulate and loop through attempts for Part 2
40-
// let noun = -1
41-
// while (noun <= maxNoun) {
42-
// let verb = -1
43-
// noun++
44-
// while (verb <= maxVerb) {
45-
// verb++
46-
// const output = tryProgram({
47-
// noun,
48-
// verb
49-
// })
50-
// // Break the search loop on success
51-
// if (output === target) {
52-
// return 100 * noun + verb
53-
// }
54-
// }
55-
// }
56-
// }
57-
24+
const part2 = () => {
25+
const data = resetInput()
26+
return validateRecords(data, undefined, 3) // Find 3 results for 2020
27+
.reduce((total, res) => total * res, 1)
28+
}
5829
const answers = []
5930
answers.push(part1())
60-
// answers.push(part2())
31+
answers.push(part2())
6132

6233
answers.forEach((ans, idx) => {
6334
console.info(`-- Part ${idx + 1} --`)

0 commit comments

Comments
 (0)