Skip to main content
-12 bytes
Source Link
user79465
user79465

Haskell, 124 105105 93 bytes

f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=(toEnum.sum.zipWith((*).(2^))[0..][7,6.reverse.take 80])s:g(drop 8s)
g.f

[Try it online!][TIO-jgcfmfe2]

[TIO-jgcfmfe2]: https://tio.run/##dYo9C8IwGIT3/IqX4pA4RHEqxQgJuLk5OJQqhaZJ6VdIUmn98zFWVw@O47k7XbpWdl0INZ6zR7YQVtuxPw9TT/GOzeS4OS1I5QXLC6TAMezHdXTRr8bcGq8x3hKKD3dC8j2lBbXyKa2T1JethJS4TOHKjgZSR5BmitahL5sBGFQjAuikBxPBTP7q7WWgOpYGEi44F9EfrfljwZPvQfC/h/AG "Haskell – Try It Online"ANQqghuCu8dj9IcGzVA6mIhUiHTgiI/@ciom30Eq/g76Nw "Haskell – Try It Online"Try it online!

f converts the string to a list of bits by comparing each character to the first one, turning the Bools into zeros and ones with fromEnum. g divides this list into groups of 8, converts them to decimal, and takes the value of the resulting number as an Enum, which Char is an instance of.

Thanks to @Laikoni for saving 19 bytes!

Changes:

  • -19 bytes thanks to @Laikoni (removing import, embedding map into function)
  • -12 bytes inspired by @Lynn's answer (getting rid of take by zipping with shorter list)

Haskell, 124 105 bytes

f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=(toEnum.sum.zipWith((*).(2^))[0..].reverse.take 8)s:g(drop 8s)
g.f

[Try it online!][TIO-jgcfmfe2]

[TIO-jgcfmfe2]: https://tio.run/##dYo9C8IwGIT3/IqX4pA4RHEqxQgJuLk5OJQqhaZJ6VdIUmn98zFWVw@O47k7XbpWdl0INZ6zR7YQVtuxPw9TT/GOzeS4OS1I5QXLC6TAMezHdXTRr8bcGq8x3hKKD3dC8j2lBbXyKa2T1JethJS4TOHKjgZSR5BmitahL5sBGFQjAuikBxPBTP7q7WWgOpYGEi44F9EfrfljwZPvQfC/h/AG "Haskell – Try It Online"ANQqghuCu8dj9IcGzVA6mIhUiHTgiI/@ciom30Eq/g76Nw "Haskell – Try It Online"

f converts the string to a list of bits by comparing each character to the first one, turning the Bools into zeros and ones with fromEnum. g divides this list into groups of 8, converts them to decimal, and takes the value of the resulting number as an Enum, which Char is an instance of.

Thanks to @Laikoni for saving 19 bytes!

Haskell, 124 105 93 bytes

f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=(toEnum.sum.zipWith((*).(2^))[7,6..0])s:g(drop 8s)
g.f

Try it online!

f converts the string to a list of bits by comparing each character to the first one, turning the Bools into zeros and ones with fromEnum. g divides this list into groups of 8, converts them to decimal, and takes the value of the resulting number as an Enum, which Char is an instance of.

Changes:

  • -19 bytes thanks to @Laikoni (removing import, embedding map into function)
  • -12 bytes inspired by @Lynn's answer (getting rid of take by zipping with shorter list)
-19 bytes
Source Link
user79465
user79465

Haskell, 124124 105 bytes

import Data.Char
f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=take 8 s:g(drop 8 s)
maps=(chrtoEnum.sum.zipWith((*).(2^))[0..].reverse).take 8)s:g(drop 8s)
g.f

Try it online! [Try it online!][TIO-jgcfmfe2]

The algorithm is pretty simple, but I feel like there's still plenty of room to golf.[TIO-jgcfmfe2]: https://tio.run/##dYo9C8IwGIT3/IqX4pA4RHEqxQgJuLk5OJQqhaZJ6VdIUmn98zFWVw@O47k7XbpWdl0INZ6zR7YQVtuxPw9TT/GOzeS4OS1I5QXLC6TAMezHdXTRr8bcGq8x3hKKD3dC8j2lBbXyKa2T1JethJS4TOHKjgZSR5BmitahL5sBGFQjAuikBxPBTP7q7WWgOpYGEi44F9EfrfljwZPvQfC/h/AG "Haskell – Try It Online"ANQqghuCu8dj9IcGzVA6mIhUiHTgiI/@ciom30Eq/g76Nw "Haskell – Try It Online"

f converts the string to a list of bits by comparing each character to the first one, turning the Bools into zeros and ones with fromEnum. g divides thethis list into groups of 8, then each resulting list (reversed to put the most significant bit last) gets convertedconverts them to decimal, which isand takes the ordinal value of the characterresulting number as an Enum, which Char is an instance of.

Thanks to @Laikoni for saving 19 bytes!

Haskell, 124 bytes

import Data.Char
f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=take 8 s:g(drop 8 s)
map(chr.sum.zipWith((*).(2^))[0..].reverse).g.f

Try it online!

The algorithm is pretty simple, but I feel like there's still plenty of room to golf. f converts the string to a list of bits by comparing each character to the first one, g divides the list into groups of 8, then each resulting list (reversed to put the most significant bit last) gets converted to decimal, which is the ordinal value of the character.

Haskell, 124 105 bytes

f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=(toEnum.sum.zipWith((*).(2^))[0..].reverse.take 8)s:g(drop 8s)
g.f

[Try it online!][TIO-jgcfmfe2]

[TIO-jgcfmfe2]: https://tio.run/##dYo9C8IwGIT3/IqX4pA4RHEqxQgJuLk5OJQqhaZJ6VdIUmn98zFWVw@O47k7XbpWdl0INZ6zR7YQVtuxPw9TT/GOzeS4OS1I5QXLC6TAMezHdXTRr8bcGq8x3hKKD3dC8j2lBbXyKa2T1JethJS4TOHKjgZSR5BmitahL5sBGFQjAuikBxPBTP7q7WWgOpYGEi44F9EfrfljwZPvQfC/h/AG "Haskell – Try It Online"ANQqghuCu8dj9IcGzVA6mIhUiHTgiI/@ciom30Eq/g76Nw "Haskell – Try It Online"

f converts the string to a list of bits by comparing each character to the first one, turning the Bools into zeros and ones with fromEnum. g divides this list into groups of 8, converts them to decimal, and takes the value of the resulting number as an Enum, which Char is an instance of.

Thanks to @Laikoni for saving 19 bytes!

Source Link
user79465
user79465

Haskell, 124 bytes

import Data.Char
f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=take 8 s:g(drop 8 s)
map(chr.sum.zipWith((*).(2^))[0..].reverse).g.f

Try it online!

The algorithm is pretty simple, but I feel like there's still plenty of room to golf. f converts the string to a list of bits by comparing each character to the first one, g divides the list into groups of 8, then each resulting list (reversed to put the most significant bit last) gets converted to decimal, which is the ordinal value of the character.