2015 day 3.
This commit is contained in:
parent
ea0c9e7812
commit
685f1e56d7
34
src/holt59/aoc/2015/day3.py
Normal file
34
src/holt59/aoc/2015/day3.py
Normal 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}")
|
@ -12,6 +12,12 @@ def main():
|
||||
parser.add_argument(
|
||||
"-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(
|
||||
"-i",
|
||||
"--input",
|
||||
@ -26,6 +32,7 @@ def main():
|
||||
|
||||
verbose: bool = args.verbose
|
||||
test: bool = args.test
|
||||
stdin: bool = args.stdin
|
||||
user: str = args.user
|
||||
input_path: Path | None = args.input
|
||||
|
||||
@ -42,8 +49,11 @@ def main():
|
||||
)
|
||||
assert input_path.exists(), f"{input_path} missing"
|
||||
|
||||
with open(input_path) as fp:
|
||||
sys.stdin = fp
|
||||
if stdin:
|
||||
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__
|
||||
|
1
src/holt59/aoc/inputs/holt59/2015/day3.txt
Normal file
1
src/holt59/aoc/inputs/holt59/2015/day3.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user