summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-03 16:56:16 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-03 16:56:54 +0000
commitd75a9d510ced0a29ad2fe7ebc247dc7b0766320c (patch)
tree3bf55422bf79ce41ac62e20da7906321c2ec8992
parentb5d1f2d79bb61afd9b03621282b7bff5f29a75f1 (diff)
downloadaoc2021-d75a9d510ced0a29ad2fe7ebc247dc7b0766320c.tar.gz
aoc2021-d75a9d510ced0a29ad2fe7ebc247dc7b0766320c.tar.xz
aoc2021-d75a9d510ced0a29ad2fe7ebc247dc7b0766320c.zip
Implement open_day utility function
-rw-r--r--1.py4
-rw-r--r--2.py4
-rw-r--r--3.py4
-rw-r--r--utils.py6
4 files changed, 14 insertions, 4 deletions
diff --git a/1.py b/1.py
index 2cc64a1..afd445d 100644
--- a/1.py
+++ b/1.py
@@ -1,5 +1,5 @@
-from utils import sliding_window
+from utils import open_day, sliding_window
-inp: list[int] = list(map(int, open('1.in')))
+inp: list[int] = list(map(int, open_day(1)))
print(sum(s[0] < s[1] for s in sliding_window(inp, 2)))
print(sum(s[0] < s[3] for s in sliding_window(inp, 4)))
diff --git a/2.py b/2.py
index 618cd86..dd33814 100644
--- a/2.py
+++ b/2.py
@@ -1,3 +1,5 @@
+from utils import open_day
+
Command = tuple[str, int]
def part1(commands: list[Command]) -> int:
@@ -23,6 +25,6 @@ def part2(commands: list[Command]) -> int:
depth += aim * X
return pos * depth
-commands: list[Command] = [(c, int(q)) for c, q in (l.split() for l in open('2.in'))]
+commands: list[Command] = [(c, int(q)) for c, q in (l.split() for l in open_day(2))]
print(part1(commands))
print(part2(commands))
diff --git a/3.py b/3.py
index c4f645c..6e77680 100644
--- a/3.py
+++ b/3.py
@@ -1,3 +1,5 @@
+from utils import open_day
+
Bits = tuple[bool, ...]
Input = list[Bits]
@@ -25,7 +27,7 @@ def part2(nums: Input) -> int:
co2: Bits = part2_impl(nums, True)
return bits_to_int(oxygen) * bits_to_int(co2)
-inp: Input = [tuple(c == '1' for c in l.strip()) for l in open('3.in')]
+inp: Input = [tuple(c == '1' for c in l.strip()) for l in open_day(3)]
print(part1(inp))
print(part2(inp))
diff --git a/utils.py b/utils.py
index d322ed0..91b90ff 100644
--- a/utils.py
+++ b/utils.py
@@ -1,6 +1,7 @@
from collections import deque
from collections.abc import Iterable, Iterator, Generator
from itertools import islice
+from sys import argv
from typing import TypeVar
T = TypeVar('T')
@@ -13,3 +14,8 @@ def sliding_window(iterable: Iterable[T], n: int) -> Generator[tuple[T, ...], No
for x in it:
window.append(x)
yield tuple(window)
+
+def open_day(n: int):
+ if len(argv) == 2:
+ return open(argv[1])
+ return open(f'{n}.in')