Here's my Haskell solution. It was farily easy today, compared to some of the others, and I took my time between part 1 and part 2, to walk the dog in what turned out to be a very wet and muddy time.
pInt::ParserIntpInt=read<$>many1digittypeRange=(Int,Int)pRange::ParserRangepRange=dol<-pIntchar'-'u<-pIntpure(l,u)pRanges::Parser(Range,Range)pRanges=dolower<-pRangespacesstring"or"spacesupper<-pRangepure(lower,upper)pField::Parser(String,(Range,Range))pField=docs<-many(noneOf":")char':'spacesr<-pRangespure(cs,r)pInts::Parser[Int]pInts=sepBy1pInt(char',')pParse::Parsera->String->apParsepsource=caseparsep""sourceofRighte->e_->error"Parsing error"withinRange::Range->Int->BoolwithinRange(l,u)v=l<=v&&v<=uinRange::Int->(Range,Range)->BoolinRangev(r,r')=withinRangerv||withinRanger'vrules::Int->[(String,(Range,Range))]->Boolrulesv=any(inRangev.snd)errorRaters=foldraux0whereauxver|rulesvrs=er|otherwise=v+erremoveInValidrs=filter((==)0.errorRaters)classify[]_=[]classify((s,r):rs)vs|all(`inRange`r)vs=s:classifyrsvs|otherwise=classifyrsvsmain=dois<-readFile"day16_input"<&>lines-- parse fields, myticket, and nearby ticketsletcs=map(pParsepField)(takeWhile(/="")is)letmyTicket=pParsepInts(head.tail$dropWhile(/="your ticket:")is)letnb=map(pParsepInts)(tail$dropWhile(/="nearby tickets:")is)-- part 1print(sum$map(errorRatecs)nb)-- part 2letvs=removeInValidcsnbfields=sortOn(length.snd)(zip[0..](map(classifycs)$transposevs))(o,m)=span(\(_,fs)->lengthfs==1)fieldsones=S.fromList.concatMapsnd$ofields'=o++snd(foldl'(\(ones,xs)(i,rs)->letrs'=filter(not.(`S.member`ones))rsin(S.insert(headrs')ones,(i,rs'):xs))(ones,[])m)depart=foldr(\ir->(myTicket!!fsti)*r)1(filter(isPrefixOf"departure".concat.snd)fields')printdepart
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.
Here's my Haskell solution. It was farily easy today, compared to some of the others, and I took my time between part 1 and part 2, to walk the dog in what turned out to be a very wet and muddy time.