import { createReadStream } from 'fs' ; import * as rl from 'readline' ; import { Cave } from './cave.class' ; import { Block } from './element/block/block.class' ; import { Ground } from './element/block/ground.class' ; import { Creature } from './element/creature/creature.class' ; import { Element } from './element/element.class' ; import { elementFactory } from './element/element.factory' ; export const reader = ( input = 'input' ) : Promise < Cave > => new Promise < Cave > ( ( res ) => { const cave = new Cave ( ) ; rl . createInterface ( { input : createReadStream ( `src/2018/day15/${ input }.txt` ) } ) . on ( 'line' , ( line ) => { if ( ! cave . width ) { cave . width = line . length ; } [ ... line ] . forEach ( ( letter , _column ) => { const element = elementFactory ( letter ) ; if ( element instanceof Creature ) { const ground = elementFactory ( '.' ) as Ground ; ground . occupant = element ; cave . grid . push ( ground ) ; } else { cave . grid . push ( element as Block ) ; } } ) ; cave . height ++ ; } ) . on ( 'close' , ( ) => { res ( cave ) ; } ) ; } ) ;