import { split } from '@lib' ; import { Bag } from './bag.class' ; export const parseLine = ( line ) : [ string , [ number , string ] [ ] | undefined ] => { const name = line . match ( /^(\w+ \w+)/ ) ?. [ 0 ] ?? '' ; const contained = line . match ( /(\d+) (\w+ \w+)/g ) ?. map ( ( [ q , ... color ] ) => [ parseInt ( q , 10 ) , color . join ( '' ) . trimStart ( ) ] as [ number , string ] ) ; return [ name , contained ] ; } ; export const parse = ( input ) : Map < string , Bag > => split ( input ) . map ( parseLine ) . reduce ( ( map , [ color , contains ] ) => { const bag = map . getOrAdd ( color , Bag . create ) ; contains ?. forEach ( ( [ count , contained ] ) => bag . canContain . set ( map . getOrAdd ( contained , Bag . create ) , count ) ) ; return map ; } , new Map < string , Bag > ( ) ) ;