2015 day 8.
This commit is contained in:
		| @@ -2,7 +2,7 @@ import logging | ||||
| import operator | ||||
| import os | ||||
| import sys | ||||
| from typing import Callable, Literal, TypeAlias, cast | ||||
| from typing import Callable | ||||
|  | ||||
| VERBOSE = os.getenv("AOC_VERBOSE") == "True" | ||||
| logging.basicConfig(level=logging.INFO if VERBOSE else logging.WARNING) | ||||
|   | ||||
							
								
								
									
										35
									
								
								src/holt59/aoc/2015/day8.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/holt59/aoc/2015/day8.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| 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}") | ||||
		Reference in New Issue
	
	Block a user