I'm using Advent of Code part 16 as an excuse to learn how to use Parsec, but I'm stumbling on how to handle this specific case.
The input is on the following format:
Before: [3, 2, 3, 0]
2 3 1 1
After: [3, 2, 3, 0]
Before: [1, 0, 2, 1]
7 0 1 1
After: [1, 1, 2, 1]
...
Before: [0, 0, 2, 1]
6 2 3 1
After: [0, 6, 2, 1]
5 0 2 3
5 1 3 1
...
5 3 2 2
In other words, first a number of groups of three lines that parse into a structure, separated by blank lines, then three blank lines, then a number of lines of four digits.
I have working parsers for each of the structures - Sample
and MaskedOperation
, with parsers sample
and maskedOp
respectively1 - but I can't figure out how to put them together to parse it into a ([Sample], [MaskedOperation])
.
I tried the following:
parseInput :: GenParser Char st ([Sample], [MaskedOperation])
parseInput = do
samples <- sample `sepBy` (count 2 newline) <* count 3 newline
operations <- maskedOp `sepBy` newline
return (samples, operations)
but it fails when it reaches the three newlines, expecting another sample:
(line 3221, column 1):
unexpected "\n"
expecting "Before:"
How do I tell parsec that I want to take as many as it can, then consume the separator (the extra newlines), then start reading other stuff?
1 Read the Advent of Code problem for context; the names are not important.