Skip to content

Commit bcdc03b

Browse files
committed
Add day 6 part 2
1 parent fb5325e commit bcdc03b

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

src/2020/day6/part2.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const part2 = (input: string) => {
2+
return input.split('\n\n').reduce((sum, group) => {
3+
const groupSize = group.split('\n').length;
4+
const allVotes = group.replace(/(\n|\s\r)/g, '');
5+
const uniqueVotes = new Set(allVotes);
6+
const voteCount = [...uniqueVotes].reduce((a, vote) => {
7+
return a + Number(allVotes.split(vote).length - 1 === groupSize);
8+
}, 0);
9+
10+
return sum + voteCount;
11+
}, 0);
12+
};

src/2020/day6/test.spec.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
import {part1} from './part1';
2+
import {part2} from './part2';
23
import {readInput} from '../../utils';
34

45
describe('Advent of Code 2020 - Day 6', () => {
6+
let testInput: string;
57
let input: string;
68
beforeAll(async () => {
9+
testInput = await readInput(__dirname + '/test_input');
710
input = await readInput(__dirname + '/input');
811
});
912

1013
describe('part 1', () => {
1114
it('should output 11 for test input', () => {
12-
const input = `abc
13-
14-
a
15-
b
16-
c
17-
18-
ab
19-
ac
15+
expect(part1(testInput)).toBe(11);
16+
});
2017

21-
a
22-
a
23-
a
24-
a
18+
it('should output 6161 from input', () => {
19+
expect(part1(input)).toBe(6161);
20+
});
21+
});
2522

26-
b
27-
`;
28-
expect(part1(input)).toBe(11);
23+
describe('part 2', () => {
24+
it('should output 6 for test input', () => {
25+
expect(part2(testInput)).toBe(6);
2926
});
3027

3128
it('should output 6161 from input', () => {
32-
expect(part1(input)).toBe(6161);
29+
expect(part2(input)).toBe(2971);
3330
});
3431
});
3532
});

src/2020/day6/test_input

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
abc
2+
3+
a
4+
b
5+
c
6+
7+
ab
8+
ac
9+
10+
a
11+
a
12+
a
13+
a
14+
15+
b

0 commit comments

Comments
 (0)