File tree Expand file tree Collapse file tree 3 files changed +40
-16
lines changed Expand file tree Collapse file tree 3 files changed +40
-16
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 11import { part1 } from './part1' ;
2+ import { part2 } from './part2' ;
23import { readInput } from '../../utils' ;
34
45describe ( '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} ) ;
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments