From 42bd8d6983bdfd555375a6f5f5c8cecb8035bfc1 Mon Sep 17 00:00:00 2001 From: Mikael CAPELLE Date: Thu, 4 Jan 2024 18:36:30 +0100 Subject: [PATCH] 2015 day 3. --- src/holt59/aoc/2015/day3.py | 34 ++++++++++++++++++++++ src/holt59/aoc/__main__.py | 16 ++++++++-- src/holt59/aoc/inputs/holt59/2015/day3.txt | 1 + 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/holt59/aoc/2015/day3.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day3.txt diff --git a/src/holt59/aoc/2015/day3.py b/src/holt59/aoc/2015/day3.py new file mode 100644 index 0000000..0c376c1 --- /dev/null +++ b/src/holt59/aoc/2015/day3.py @@ -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}") diff --git a/src/holt59/aoc/__main__.py b/src/holt59/aoc/__main__.py index ba8da17..4dc3ecb 100644 --- a/src/holt59/aoc/__main__.py +++ b/src/holt59/aoc/__main__.py @@ -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__ diff --git a/src/holt59/aoc/inputs/holt59/2015/day3.txt b/src/holt59/aoc/inputs/holt59/2015/day3.txt new file mode 100644 index 0000000..f7791cc --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day3.txt @@ -0,0 +1 @@ +v>vvv>v<<<^^^^^<<^^>v^>^>^>^>^>^<<^><<<^vvvv>^>^><^v^><^<>^^>^vvv^>^>^^<>><>^>vvv>>^vv>^<><>^^>^>><<^><><>^<^>>vvv>v>>>v<<^<><^v>^^v^^^<^v^^>>><^>^>v<>^<>>^>^^v^>>><>v^v<>>^>^<>v^>^<>^v^^^v^^>>vv<<^^><^^>^^<^>>^^^^^v^vv<>>v^v<^v^^<><^<^vv^><>><><>v>vvv^vv^^<<><<<^v^>vvv^<^>vvvv^>^>>^v^v>vv^<>><^^^>^>>v>^>v^<>v><^<^^^vv<^^<>v^v^vv<>>>>v^v<>><^^v>vv^^>v^v>v>v>>vv>^^>^v><<^<vv^^^v>v^^^>><^^>v>^^v>>v^^^<^^v>^v>><^<^<>>v<<^^vv>^^^v<^<^^vv^>>v^>><<<>^vv^<^<>v^^<<^><>>^^^<^vv<^^^>><^^v>^^v^^^^<^v<^<<<<^v^<^^<>^^>^><<>>^v><>><^<^^^>>vv>^>^^^^^v^vvv><><^<^>v>v^v^>^><><^<^><>v<><>^v^^v>^<<<>^v^>^<v^<>>^vv>v>>>^<^>>>>>v>>^v>v><>>vvv<^^><<^>^>v<^vvvv<^^^v^^^>v^v<>v<^^v>>><>v>v>>^^<^^v><<<<<^vv<^<>^>>>^v>^v>vv>^v<>v>v<^>>v>>^>^><^^v<><><^^>^^^^>vv^v^v>^v^^v^^v>><^v>>vv<>vvvv<>>^v^>^>>v^v^<<>>^v<^^vv^><>v>^>v><<<<<<<^>^^v^<<^^>>vvv^<><>><>^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