My Haskell soloution, have to save I've never used bitwise operations in Haskell very much, so had to look all that up. And during that search I realised that foldl', which I defined on day 8, is actually implemented in Data.List, oops.. :-)
dataInst=MaskIntegerInteger-- mask with off bits and mask with on bits|MemIntegerIntegerderiving(Show,Eq)typeProgram=[Inst]parse::[String]->Programparse=mapauxwhereaux('m':'a':'s':'k':' ':'=':' ':mask)=let(off,""):_=readInt2(constTrue)(fromEnum.(=='1'))mask(on,""):_=readInt2(constTrue)(fromEnum.(/='0'))maskinMaskonoffauxxs=letidx=read(takeWhileisDigit(drop4xs))::Integern=read(drop2(dropWhile(/='=')xs))::IntegerinMemidxntask1::[Inst]->Integertask1=M.foldr(+)0.snd.foldl'(uncurryaux)((0,0),M.empty)whereaux_mem(Maskonoff)=((on,off),mem)aux(on,off)mem(Memidxvalue)=((on,off),M.insertidx(value.&.on.|.off)mem)task2::[Inst]->Integertask2=M.foldr(+)0.snd.foldl'(uncurryaux)((0,0),M.empty)whereaux_mem(Maskonoff)=((on,off),mem)aux(on,off)mem(Memidxvalue)=((on,off),letidxs=map(xor(idx.|.off))(bitPowerSet(off`xor`on))infoldl'(\memidx->M.insertidxvaluemem)memidxs)-- function from redditbitPowerSet::Integer->[Integer]bitPowerSetbits=chooseBits<$>[0..1`shiftL`popCountbits-1]wherechooseBitsi=fst$foldl'(fi)(0,bits)[0..popCountbits-1]fi(x,k)j=(x`xor`bits.&.(k`xor`(k-i`shiftR`j.&.1)),k.&.(k-1))main::IO()main=doinsts<-readFile"day14_input"<&>lines<&>parseprint(task1insts)print(task2insts)
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.
My Haskell soloution, have to save I've never used bitwise operations in Haskell very much, so had to look all that up. And during that search I realised that foldl', which I defined on day 8, is actually implemented in Data.List, oops.. :-)