Copyright | (c) Eric Mertens 2021 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
https://adventofcode.com/2021/day/16
Decode an expression from a bitstream.
This solution uses the ReadP parser combinator module.
Since ReadP only operates on String
parsing bits is
simulated by parsing strings of the characters '0'
and '1'
.
ReadP's gather
operation is useful for computing
the bitsize of a packet which comes up in some of the
operator packets.
Examples
>>>
vers <$> parse (decodeHex "8A004A801A8002F478")
Just 16
>>>
vers <$> parse (decodeHex "620080001611562C8802118E34")
Just 12
>>>
vers <$> parse (decodeHex "C0015000016115A2E0802F182340")
Just 23
>>>
vers <$> parse (decodeHex "A0016C880162017C3686B18A3D4780")
Just 31
>>>
eval <$> parse (decodeHex "C200B40A82")
Just 3
>>>
eval <$> parse (decodeHex "04005AC33890")
Just 54
>>>
eval <$> parse (decodeHex "880086C3E88112")
Just 7
>>>
eval <$> parse (decodeHex "CE00C43D881120")
Just 9
>>>
eval <$> parse (decodeHex "D8005AC2A8F0")
Just 1
>>>
eval <$> parse (decodeHex "F600BC2D8F")
Just 0
>>>
eval <$> parse (decodeHex "9C005AC2F8F0")
Just 0
>>>
eval <$> parse (decodeHex "9C0141080250320F1802104A08")
Just 1
Documentation
A BITS packet
parse :: String -> Maybe Packet Source #
Parser for strings of '1'
and '0'
to Packet
>>>
parse "110100101111111000101000"
Just (Lit 6 2021)
>>>
parse "00111000000000000110111101000101001010010001001000000000"
Just (Op 1 6 [Lit 6 10,Lit 2 20])
>>>
parse "11101110000000001101010000001100100000100011000001100000"
Just (Op 7 3 [Lit 2 1,Lit 4 2,Lit 1 3])
decodeHex :: String -> String Source #
Decode a hex string into bit string
>>>
decodeHex "D2FE28"
"110100101111111000101000"
>>>
decodeHex "38006F45291200"
"00111000000000000110111101000101001010010001001000000000"
ReadP
parser combinators
pArguments :: ReadP [Packet] Source #
Parse a list of sub-packets either by packet count or bit-size