Skip to content

Commit 89b12db

Browse files
feat(2020-day-06): calculate checksum of a given group's answers
1 parent 212df0d commit 89b12db

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

2020/day-06/questions.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const alphabet = [...'abcdefghijklmnopqrstuvwxyz']
2+
3+
/**
4+
* Counts which questions were answered by
5+
* at least one passenger in the group.
6+
* Multiple people answering doesn't matter.
7+
*/
8+
const groupChecksum = (answers) => {
9+
return alphabet.reduce((sum, ltr) => {
10+
if (String(answers).includes(ltr)) { sum++ }
11+
return sum
12+
}, 0)
13+
}
14+
15+
module.exports = {
16+
groupChecksum
17+
}

2020/day-06/questions.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { groupChecksum } = require('./questions')
4+
5+
const testData = {
6+
groups: [
7+
`abcx
8+
abcy
9+
abcz`,
10+
'abc',
11+
`a
12+
b
13+
c`,
14+
`ab
15+
ac`,
16+
`a
17+
a
18+
a
19+
a`,
20+
'b'
21+
],
22+
checksums: [6, 3, 3, 3, 1, 1]
23+
}
24+
25+
describe('--- Day 6: Custom Customs ---', () => {
26+
describe('Part 1', () => {
27+
describe('groupChecksum()', () => {
28+
it('tallies the number of unique questions answered collectively by a group', () => {
29+
testData.groups.forEach((group, idx) => {
30+
expect(groupChecksum(group)).to.deep.equal(testData.checksums[idx])
31+
})
32+
})
33+
})
34+
})
35+
})

0 commit comments

Comments
 (0)