Ah today was fun... I did have to stop and think for part 2 as I'd decided to go down the path of parser combinators rather than regex and it was clearly to late to backtrack! Anywhy managed to handle them directly, I guess it's a bit of hack, but worked out fine.
dataRule=RS[Int](Maybe[Int])|RCCharderiving(Eq,Show)quotedString::ParserRulequotedString=doTP.char'"'xs<-TP.many(TP.noneOf"\""TP.<|>(TP.char'\\'>>TP.char'\"'))TP.char'"'return$RC(headxs)number::ParserIntnumber=dodigits<-many1TP.digitTP.spacesreturn$readdigitsruleSeq::ParserRuleruleSeq=dols<-many1(TP.spaces>>number)rs<-optionMaybe(TP.spaces>>TP.char'|'>>many1(TP.spaces>>number))return$RSlsrsrule::Parser(Int,Rule)rule=doi<-numberTP.char':'r<-TP.try(TP.spaces>>quotedString)TP.<|>ruleSeqreturn(i,r)parse::Parsera->String->aparseps=caseTP.parse(TP.spaces*>p<*eof)""sofLefte->error$showeRightx->xbuild::M.MapIntRule->Rule->Parser()buildm(RSlsrs)=choice(TP.try(auxls):maybe[](\rs->[auxrs])rs)whereaux[n]=buildm(mM.!n)aux(n:xs)=do_<-buildm(mM.!n)_<-auxxsreturn()build_(RCc)=void(TP.charc)main=do(rs,is)<-readFile"day19_input"<&>lines<&>span(/="")<&>bimap(M.fromList.map(parserule))tailletzero=rsM.!0g=isRight.TP.parse(TP.spaces*>buildrszero<*eof)""print(length$filterid$mapgis)-- part 2 (we need to be a little careful to not get stuck in that loop :-))letp42=buildrs(rsM.!42)p31=buildrs(rsM.!31)p=dor42<-TP.many1$TP.tryp42r31<-TP.many1$TP.tryp31iflengthr42>lengthr31thenreturn()elsefail"no luck"g'=isRight.TP.parse(TP.spaces*>p<*eof)""print(length$filterid$mapg'is)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ah today was fun... I did have to stop and think for part 2 as I'd decided to go down the path of parser combinators rather than regex and it was clearly to late to backtrack! Anywhy managed to handle them directly, I guess it's a bit of hack, but worked out fine.