2015 day 3.

This commit is contained in:
Mikael CAPELLE 2024-01-04 18:36:30 +01:00
parent 0567ab7440
commit 42bd8d6983
3 changed files with 48 additions and 3 deletions

View File

@ -0,0 +1,34 @@
import sys
from collections import defaultdict
line = sys.stdin.read().strip()
def process(directions: str) -> dict[tuple[int, int], int]:
counts: dict[tuple[int, int], int] = defaultdict(lambda: 0)
counts[0, 0] = 1
x, y = (0, 0)
for c in directions:
match c:
case ">":
x += 1
case "<":
x -= 1
case "^":
y -= 1
case "v":
y += 1
case _:
raise ValueError()
counts[x, y] += 1
return counts
answer_1 = len(process(line))
print(f"answer 1 is {answer_1}")
answer_2 = len(process(line[::2]) | process(line[1::2]))
print(f"answer 2 is {answer_2}")

View File

@ -12,6 +12,12 @@ def main():
parser.add_argument( parser.add_argument(
"-u", "--user", type=str, default="holt59", help="user input to use" "-u", "--user", type=str, default="holt59", help="user input to use"
) )
parser.add_argument(
"--stdin",
action="store_true",
default=False,
help="use stdin as input",
)
parser.add_argument( parser.add_argument(
"-i", "-i",
"--input", "--input",
@ -26,6 +32,7 @@ def main():
verbose: bool = args.verbose verbose: bool = args.verbose
test: bool = args.test test: bool = args.test
stdin: bool = args.stdin
user: str = args.user user: str = args.user
input_path: Path | None = args.input input_path: Path | None = args.input
@ -42,8 +49,11 @@ def main():
) )
assert input_path.exists(), f"{input_path} missing" assert input_path.exists(), f"{input_path} missing"
with open(input_path) as fp: if stdin:
sys.stdin = fp
importlib.import_module(f".{year}.day{day}", __package__) importlib.import_module(f".{year}.day{day}", __package__)
else:
with open(input_path) as fp:
sys.stdin = fp
importlib.import_module(f".{year}.day{day}", __package__)
sys.stdin = sys.__stdin__ sys.stdin = sys.__stdin__

File diff suppressed because one or more lines are too long