Last active
August 6, 2019 08:37
-
-
Save shanecharles/3fa2f0437d05e0b1c3b3de2aadeab834 to your computer and use it in GitHub Desktop.
FParsec code to parse Advent of Code day 16 input to an Operation choice.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "packages/FParsec/lib/net40-client/FParsecCS.dll" | |
#r "packages/FParsec/lib/net40-client/FParsec.dll" | |
open FParsec | |
type Operation = | |
| Spin of int | |
| Exchange of int * int | |
| Partner of char * char | |
type ParserState = unit | |
let pspin : Parser<Operation,ParserState> = | |
pchar 's' >>. pint32 |>> Spin | |
let pexchange = | |
pchar 'x' | |
>>. tuple2 (pint32 .>> pchar '/') pint32 | |
|>> Exchange | |
let ppartner = | |
pchar 'p' | |
>>. tuple2 (asciiLetter .>> pchar '/') asciiLetter | |
|>> Partner | |
let parseOperation = pspin <|> pexchange <|> ppartner | |
["po/k"; "x4/0"; "s12"] |> List.map (run parseOperation) | |
// Success: Partner ('o','k'); Success: Exchange (4,0); Success: Spin 12] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment