36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
import logging
|
|
import os
|
|
import sys
|
|
|
|
VERBOSE = os.getenv("AOC_VERBOSE") == "True"
|
|
logging.basicConfig(level=logging.INFO if VERBOSE else logging.WARNING)
|
|
|
|
|
|
lines = sys.stdin.read().splitlines()
|
|
|
|
answer_1 = 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
|
|
)
|
|
print(f"answer 1 is {answer_1}")
|
|
|
|
answer_2 = 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
|
|
)
|
|
print(f"answer 2 is {answer_2}")
|