-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
2,243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.