from typing import Any, Iterator from ..base import BaseSolver class Solver(BaseSolver): def solve(self, input: str) -> Iterator[Any]: lines = input.splitlines() yield sum( # left and right quotes (not in memory) 2 # each \\ adds one character in the literals (compared to memory) + line.count(R"\\") # each \" adds one character in the literals (compared to memory) + line[1:-1].count(R"\"") # each \xFF adds 3 characters in the literals (compared to memory), but we must not # count A\\x (A != \), but we must count A\\\x (A != \) - in practice we should also # avoid \\\\x, etc., but this does not occur in the examples and the actual input + 3 * (line.count(R"\x") - line.count(R"\\x") + line.count(R"\\\x")) for line in lines ) yield sum( # needs to wrap in quotes (2 characters) 2 # needs to escape every \ with an extra \ + line.count("\\") # needs to escape every " with an extra \ (including the first and last ones) + line.count('"') for line in lines )