Expand description
Β§Matchsticks
While regular expressions may feel like a
natural choice, itβs much faster and easier to simply treat the input as a stream of raw
ASCII u8
bytes including newlines.
For part one we run a small state machine using fold
to keep track of the current and
previous characters. If we encounter a hexadecimal escape then four characters become one so the
difference increases by three. The sequences \\
and \"
both increase the difference by one.
Each newline increases the difference by two since every line is enclosed with two quotes.
Part two is even more straightforward with no need for statekeeping. Quotes and backslashes need to be escaped so increase the difference by one. As before each newline increases by the difference by two.