Skip to content

Commit

Permalink
feat: 2020/6
Browse files Browse the repository at this point in the history
Add day 6 of 2020
  • Loading branch information
marcobiedermann committed Dec 26, 2020
1 parent 974a194 commit 7da2eeb
Show file tree
Hide file tree
Showing 5 changed files with 2,243 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2020/day/6/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { readFileSync } from 'fs';
import { part1, part2 } from '.';

const input = readFileSync(`${__dirname}/input`, 'utf-8').split('\n\n');
const sampleInput = readFileSync(`${__dirname}/input.sample`, 'utf-8').split('\n\n');

describe('Day 6', () => {
describe('Part 1', () => {
it('should return count of some answers', () => {
expect.assertions(2);

expect(part1(sampleInput)).toStrictEqual(11);
expect(part1(input)).toStrictEqual(6161);
});
});

describe('Part 2', () => {
it('should return count of all answers', () => {
expect.assertions(2);

expect(part2(sampleInput)).toStrictEqual(6);
expect(part2(input)).toStrictEqual(2971);
});
});
});
33 changes: 33 additions & 0 deletions 2020/day/6/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function union<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a, ...b]);
}

function intersection<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a].filter((element) => b.has(element)));
}

function part1(groups: string[]): number {
return groups.reduce(
(totalGroups, group) =>
totalGroups +
group
.split('\n')
.map((person) => new Set(person))
.reduce((totalPeople, person) => union(totalPeople, new Set(person))).size,
0,
);
}

function part2(groups: string[]): number {
return groups.reduce(
(totalGroups, group) =>
totalGroups +
group
.split('\n')
.map((person) => new Set(person))
.reduce((totalPeople, person) => intersection(totalPeople, new Set(person))).size,
0,
);
}

export { part1, part2 };
Loading

0 comments on commit 7da2eeb

Please sign in to comment.