File tree Expand file tree Collapse file tree 5 files changed +62
-3
lines changed Expand file tree Collapse file tree 5 files changed +62
-3
lines changed Original file line number Diff line number Diff line change 1- import { part1 } from './part1' ;
21import { readInput } from '../../utils' ;
32
3+ import { part1 } from './part1' ;
4+
45describe ( 'Advent of Code 2020 - Day x' , ( ) => {
56 let input : string ;
67 beforeAll ( async ( ) => {
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ type Bags = Record<string, string[]>;
22
33const parseColor = ( rule : string ) => rule . replace ( / \d | b a g ( s ? .) ? / g, '' ) . trim ( ) ;
44
5- const parseBags = ( input : string ) : Bags => {
5+ export const parseBags = ( input : string ) : Bags => {
66 return input . split ( '\n' ) . reduce ( ( bags , r ) => {
77 const [ , color , rules ] = r . match ( / ( [ a - z ] + [ a - z ] + ) b a g s c o n t a i n ( .* ) / ) ! ;
88 return { ...bags , [ color ] : rules . split ( ', ' ) } ;
Original file line number Diff line number Diff line change 1+ import { parseBags } from './part1' ;
2+
3+ const parseAmount = ( rule : string ) => Number ( rule . match ( / \d / ) ) ;
4+ const parseColor = ( rule : string ) => {
5+ const color = rule . replace ( / \d | b a g ( s ? .? ) ? / g, '' ) . trim ( ) ;
6+ return ! color . includes ( 'no other' ) ? color : '' ;
7+ } ;
8+
9+ export const part2 = ( input : string ) => {
10+ const bags = parseBags ( input ) ;
11+ const bagsToCount = [ 'shiny gold' ] ;
12+ let sum = 0 ;
13+
14+ while ( bagsToCount . length > 0 ) {
15+ const bag = bagsToCount . pop ( ) ! ;
16+ const bagsCounter = bags [ bag ] . reduce ( ( a , rule ) => {
17+ const amount = parseAmount ( rule ) ;
18+
19+ bagsToCount . push (
20+ ...Array ( amount )
21+ . fill ( parseColor ( rule ) )
22+ . filter ( a => a ) // remove '' (no other bags)
23+ ) ;
24+
25+ return a + amount ;
26+ } , 0 ) ;
27+
28+ sum += bagsCounter ;
29+ }
30+
31+ return sum ;
32+ } ;
Original file line number Diff line number Diff line change 1- import { part1 } from './part1' ;
21import { readInput } from '../../utils' ;
32
3+ import { part1 } from './part1' ;
4+ import { part2 } from './part2' ;
5+
46describe ( 'Advent of Code 2020 - Day 7' , ( ) => {
57 let testInput : string ;
8+ let testInput2 : string ;
69 let input : string ;
10+
711 beforeAll ( async ( ) => {
812 testInput = await readInput ( __dirname + '/test_input' ) ;
13+ testInput2 = await readInput ( __dirname + '/test_input_2' ) ;
914 input = await readInput ( __dirname + '/input' ) ;
1015 } ) ;
1116
@@ -18,4 +23,18 @@ describe('Advent of Code 2020 - Day 7', () => {
1823 expect ( part1 ( input ) ) . toBe ( 289 ) ;
1924 } ) ;
2025 } ) ;
26+
27+ describe ( 'part 2' , ( ) => {
28+ it ( 'should output 32 from test_input' , ( ) => {
29+ expect ( part2 ( testInput ) ) . toBe ( 32 ) ;
30+ } ) ;
31+
32+ it ( 'should output 126 from test_input2' , ( ) => {
33+ expect ( part2 ( testInput2 ) ) . toBe ( 126 ) ;
34+ } ) ;
35+
36+ it ( 'should output 30055 valid bags from input' , ( ) => {
37+ expect ( part2 ( input ) ) . toBe ( 30055 ) ;
38+ } ) ;
39+ } ) ;
2140} ) ;
Original file line number Diff line number Diff line change 1+ shiny gold bags contain 2 dark red bags.
2+ dark red bags contain 2 dark orange bags.
3+ dark orange bags contain 2 dark yellow bags.
4+ dark yellow bags contain 2 dark green bags.
5+ dark green bags contain 2 dark blue bags.
6+ dark blue bags contain 2 dark violet bags.
7+ dark violet bags contain no other bags.
You can’t perform that action at this time.
0 commit comments