File tree Expand file tree Collapse file tree 7 files changed +713
-1
lines changed Expand file tree Collapse file tree 7 files changed +713
-1
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 ( ) => {
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1+ type Bags = Record < string , string [ ] > ;
2+
3+ const parseColor = ( rule : string ) => rule . replace ( / \d | b a g ( s ? .) ? / g, '' ) . trim ( ) ;
4+
5+ export const parseBags = ( input : string ) : Bags => {
6+ return input . split ( '\n' ) . reduce ( ( bags , r ) => {
7+ const [ , color , rules ] = r . match ( / ( [ a - z ] + [ a - z ] + ) b a g s c o n t a i n ( .* ) / ) ! ;
8+ return { ...bags , [ color ] : rules . split ( ', ' ) } ;
9+ } , { } ) ;
10+ } ;
11+
12+ export const part1 = ( input : string ) => {
13+ const bags = parseBags ( input ) ;
14+
15+ const checkBag = ( color : keyof Bags ) : boolean => {
16+ const rules = bags [ color ] ;
17+
18+ if ( rules . join ( ) . includes ( 'no other bags' ) ) return false ;
19+ if ( rules . join ( ) . includes ( 'shiny gold' ) ) return true ;
20+
21+ // Check if the other bags conain the target
22+ return ! ! rules . filter ( rule => checkBag ( parseColor ( rule ) ) ) . length ;
23+ } ;
24+
25+ // Check all bags if valid
26+ const validBags = new Set ( ) ;
27+ for ( const bag in bags ) checkBag ( bag ) && validBags . add ( bag ) ;
28+ return validBags . size ;
29+ } ;
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 { readInput } from '../../utils' ;
2+
3+ import { part1 } from './part1' ;
4+ import { part2 } from './part2' ;
5+
6+ describe ( 'Advent of Code 2020 - Day 7' , ( ) => {
7+ let testInput : string ;
8+ let testInput2 : string ;
9+ let input : string ;
10+
11+ beforeAll ( async ( ) => {
12+ testInput = await readInput ( __dirname + '/test_input' ) ;
13+ testInput2 = await readInput ( __dirname + '/test_input_2' ) ;
14+ input = await readInput ( __dirname + '/input' ) ;
15+ } ) ;
16+
17+ describe ( 'part 1' , ( ) => {
18+ it ( 'should output 4' , ( ) => {
19+ expect ( part1 ( testInput ) ) . toBe ( 4 ) ;
20+ } ) ;
21+
22+ it ( 'should output 289 valid bags from input' , ( ) => {
23+ expect ( part1 ( input ) ) . toBe ( 289 ) ;
24+ } ) ;
25+ } ) ;
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+ } ) ;
40+ } ) ;
Original file line number Diff line number Diff line change 1+ light red bags contain 1 bright white bag, 2 muted yellow bags.
2+ dark orange bags contain 3 bright white bags, 4 muted yellow bags.
3+ bright white bags contain 1 shiny gold bag.
4+ muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
5+ shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
6+ dark olive bags contain 3 faded blue bags, 4 dotted black bags.
7+ vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
8+ faded blue bags contain no other bags.
9+ dotted black bags contain no other bags.
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