package Ġme . pe ck b . a oc ._ 2 0 1 8 . calendar . day 1 4 Ċ Ċ import Ġjavax . inject . Inject Ċ Ċ import Ġme . pe ck b . a oc . generators . Input Generator . Input Generator Factory Ċ import Ġjava . lang . StringBuilder Ċ Ċ class ĠDay 1 4 Ġ@ Inject Ġconstructor ( private Ġval Ġgenerator Factory : ĠInput Generator Factory ) Ġ{ ĊĠ Ġfun Ġpart One ( filename : ĠString ) Ġ= Ġgenerator Factory . for File ( filename ). read One Ġ{ Ġinput Ġ-> ĊĠĠĠ Ġval Ġbest E lf Guess Ġ= Ġinput . toInt () ĊĠĠĠ Ġval Ġcho colate Recipe One Ġ= ĠNode ( 3 ) ĊĠĠĠ Ġval Ġcho colate Recipe Two Ġ= ĠNode ( 7 ). apply Ġ{ ĊĠĠĠĠĠ Ġparent Ġ= Ġcho colate Recipe One ĊĠĠĠĠĠ Ġchild Ġ= Ġcho colate Recipe One ĊĠĠĠĠĠ Ġcho colate Recipe One . child Ġ= Ġthis ĊĠĠĠĠĠ Ġcho colate Recipe One . parent Ġ= Ġthis ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġvar Ġfirst E lf Pointer Ġ= Ġcho colate Recipe One ĊĠĠĠ Ġvar Ġsecond E lf Pointer Ġ= Ġcho colate Recipe Two ĊĠĠĠ Ġvar Ġlast Element Ġ= Ġcho colate Recipe Two ĊĊĠĠĠ Ġvar Ġrecipes Made Ġ= Ġ 2 ĊĠĠĠ Ġwhile ( recipes Made Ġ< Ġbest E lf Guess Ġ+ Ġ 1 0 ) Ġ{ ĊĠĠĠĠĠ Ġvar Ġnew Recipe Score Ġ= Ġfirst E lf Pointer Ġ+ Ġsecond E lf Pointer ĊĠĠĠĠĠ Ġif Ġ( new Recipe Score Ġ>= Ġ 1 0 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġt ens Node Ġ= ĠNode ( 1 ). insert After ( last Element ) ĊĠĠĠĠĠĠĠ Ġlast Element Ġ= Ġt ens Node ĊĠĠĠĠĠĠĠ Ġnew Recipe Score Ġ-= Ġ 1 0 ĊĠĠĠĠĠĠĠ Ġrecipes Made ++ ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġval Ġones Node Ġ= ĠNode ( new Recipe Score ). insert After ( last Element ) ĊĠĠĠĠĠ Ġlast Element Ġ= Ġones Node ĊĠĠĠĠĠ Ġrecipes Made ++ ĊĊĠĠĠĠĠ Ġrepeat ( first E lf Pointer Ġ+ Ġ 1 ) Ġ{ Ġfirst E lf Pointer Ġ= Ġfirst E lf Pointer . child Ġ} ĊĠĠĠĠĠ Ġrepeat ( second E lf Pointer Ġ+ Ġ 1 ) Ġ{ Ġsecond E lf Pointer Ġ= Ġsecond E lf Pointer . child Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġif Ġ( recipes Made Ġ> Ġbest E lf Guess Ġ+ Ġ 1 0 ) Ġ{ ĊĠĠĠĠĠ Ġlast Element Ġ= Ġlast Element . parent ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġsb Ġ= ĠStringBuilder () ĊĠĠĠ Ġrepeat ( 1 0 ) Ġ{ ĊĠĠĠĠĠ Ġsb . insert ( 0 , Ġlast Element . value ) ĊĠĠĠĠĠ Ġlast Element Ġ= Ġlast Element . parent ĊĠĠĠ Ġ} ĊĠĠĠ Ġsb . toString () ĊĠ Ġ} ĊĊĠ Ġfun Ġpart Two ( filename : ĠString ) Ġ= Ġgenerator Factory . for File ( filename ). read One Ġ{ Ġwanted Re cip ies Ġ-> ĊĠĠĠ Ġval Ġcho colate Recipe One Ġ= ĠNode ( 3 ) ĊĠĠĠ Ġval Ġcho colate Recipe Two Ġ= ĠNode ( 7 ). apply Ġ{ ĊĠĠĠĠĠ Ġparent Ġ= Ġcho colate Recipe One ĊĠĠĠĠĠ Ġchild Ġ= Ġcho colate Recipe One ĊĠĠĠĠĠ Ġcho colate Recipe One . child Ġ= Ġthis ĊĠĠĠĠĠ Ġcho colate Recipe One . parent Ġ= Ġthis ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġvar Ġfirst E lf Pointer Ġ= Ġcho colate Recipe One ĊĠĠĠ Ġvar Ġsecond E lf Pointer Ġ= Ġcho colate Recipe Two ĊĠĠĠ Ġvar Ġlast Element Ġ= Ġcho colate Recipe Two ĊĊĠĠĠ Ġvar Ġfound Recipes Ġ= Ġfalse ĊĠĠĠ Ġvar Ġrecipes Made Ġ= Ġ 2 ĊĠĠĠ Ġwhile (! found Recipes ) Ġ{ ĊĠĠĠĠĠ Ġvar Ġnew Recipe Score Ġ= Ġfirst E lf Pointer Ġ+ Ġsecond E lf Pointer ĊĠĠĠĠĠ Ġif Ġ( new Recipe Score Ġ>= Ġ 1 0 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġt ens Node Ġ= ĠNode ( 1 ). insert After ( last Element ) ĊĠĠĠĠĠĠĠ Ġlast Element Ġ= Ġt ens Node ĊĠĠĠĠĠĠĠ Ġnew Recipe Score Ġ-= Ġ 1 0 ĊĠĠĠĠĠĠĠ Ġrecipes Made ++ ĊĠĠĠĠĠĠĠ Ġfound Recipes Ġ= Ġcheck Last ( wanted Re cip ies , Ġlast Element ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġval Ġones Node Ġ= ĠNode ( new Recipe Score ). insert After ( last Element ) ĊĠĠĠĠĠ Ġlast Element Ġ= Ġones Node ĊĠĠĠĠĠ Ġrecipes Made ++ ĊĊĠĠĠĠĠ Ġrepeat ( first E lf Pointer Ġ+ Ġ 1 ) Ġ{ Ġfirst E lf Pointer Ġ= Ġfirst E lf Pointer . child Ġ} ĊĠĠĠĠĠ Ġrepeat ( second E lf Pointer Ġ+ Ġ 1 ) Ġ{ Ġsecond E lf Pointer Ġ= Ġsecond E lf Pointer . child Ġ} ĊĠĠĠĠĠ Ġfound Recipes Ġ= Ġfound Recipes Ġ|| Ġcheck Last ( wanted Re cip ies , Ġlast Element ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġif Ġ( last Element . value Ġ== ĠCharacter . get Numeric Value ( wanted Re cip ies . last ())) Ġ{ ĊĠĠĠĠĠ Ġrecipes Made Ġ- Ġwanted Re cip ies . length ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġrecipes Made Ġ- Ġ 1 Ġ- Ġwanted Re cip ies . length ĊĠĠĠ Ġ} ĊĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcheck Last ( wanted Recipes : ĠString , Ġlast Element : ĠNode ): ĠBoolean Ġ{ ĊĠĠĠ Ġvar Ġmoving Pointer Ġ= Ġlast Element ĊĠĠĠ Ġreturn Ġwanted Recipes . indices . all Ġ{ Ġindex Ġ-> ĊĠĠĠĠĠ Ġmoving Pointer . value Ġ== ĠCharacter . get Numeric Value ( wanted Recipes [ wanted Recipes . length Ġ- Ġ 1 Ġ- Ġindex ]). also Ġ{ ĊĠĠĠĠĠĠĠ Ġmoving Pointer Ġ= Ġmoving Pointer . parent ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġdata Ġclass ĠNode ( val Ġvalue : ĠInt ) Ġ{ ĊĠĠĠ Ġlateinit Ġvar Ġparent : ĠNode ĊĠĠĠ Ġlateinit Ġvar Ġchild : ĠNode ĊĊĠĠĠ Ġin fix Ġoperator Ġfun Ġplus ( other : ĠNode ) Ġ= Ġvalue Ġ+ Ġother . value ĊĠĠĠ Ġin fix Ġoperator Ġfun Ġplus ( other : ĠInt ) Ġ= Ġvalue Ġ+ Ġother ĊĊĠĠĠ Ġfun Ġinsert After ( new Parent : ĠNode ) Ġ= Ġapply Ġ{ ĊĠĠĠĠĠ Ġparent Ġ= Ġnew Parent ĊĠĠĠĠĠ Ġchild Ġ= Ġnew Parent . child ĊĠĠĠĠĠ Ġnew Parent . child Ġ= Ġthis ĊĠĠĠĠĠ Ġchild . parent Ġ= Ġthis ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ