diff options
Diffstat (limited to '2020/16')
-rwxr-xr-x | 2020/16/puzzle-1.py | 8 | ||||
-rwxr-xr-x | 2020/16/puzzle-2.py | 33 |
2 files changed, 11 insertions, 30 deletions
diff --git a/2020/16/puzzle-1.py b/2020/16/puzzle-1.py index f4343c8..9f35856 100755 --- a/2020/16/puzzle-1.py +++ b/2020/16/puzzle-1.py @@ -2,7 +2,7 @@ def main() -> None: - with open("input", "r") as f: + with open("input", "r", encoding="utf-8") as f: data = f.readlines() i = 0 @@ -11,8 +11,7 @@ def main() -> None: ranges = data[i].split(": ")[1].split(" or ") for _range in ranges: bounds = tuple(map(int, _range.split("-"))) - for j in range(bounds[0], bounds[1] + 1): - valid.append(j) + valid.extend(range(bounds[0], bounds[1] + 1)) i += 1 # Skip to nearby tickets @@ -20,8 +19,7 @@ def main() -> None: acc = 0 for j in range(i, len(data)): - fields = tuple(map(int, data[j].split(","))) - acc += sum(field for field in fields if field not in valid) + acc += sum(field for field in tuple(map(int, data[j].split(","))) if field not in valid) print(acc) diff --git a/2020/16/puzzle-2.py b/2020/16/puzzle-2.py index 1060049..40416f6 100755 --- a/2020/16/puzzle-2.py +++ b/2020/16/puzzle-2.py @@ -5,14 +5,11 @@ from operator import mul def not_reduced(label_dict: dict[int, str]) -> bool: - for i in label_dict: - if len(label_dict[i]) != 1: - return True - return False + return any(len(label_dict[i]) != 1 for i in label_dict) def main() -> None: - with open("input", "r") as f: + with open("input", "r", encoding="utf-8") as f: data = f.readlines() i = 0 @@ -23,6 +20,7 @@ def main() -> None: ranges = data[i].split(": ")[1].split(" or ") for _range in ranges: bounds = tuple(map(int, _range.split("-"))) + valid.extend(range(bounds[0], bounds[1] + 1)) for j in range(bounds[0], bounds[1] + 1): valid.append(j) i += 1 @@ -33,36 +31,21 @@ def main() -> None: vtickets: list[tuple[int, ...]] = [] for j in range(i, len(data)): fields = tuple(map(int, data[j].split(","))) - check = True - for field in fields: - if field not in valid: - check = False - break - if check == True: - vtickets.append(tuple(map(int, data[j].split(",")))) + if all(field in valid for field in fields) == True: + vtickets.append(fields) - label_dict: dict[int, list[str]] = {} - for i in range(len(labels)): - label_dict[i] = [] + label_dict: dict[int, list[str]] = {i: [] for i in range(len(labels))} - # Triple for loop, gross! for label in labels: valid: list[list[int]] = [] ranges = data[labels.index(label)].split(": ")[1].split(" or ") for _range in ranges: bounds = tuple(map(int, _range.split("-"))) - for i in range(bounds[0], bounds[1] + 1): - valid.append(i) + valid.extend(range(bounds[0], bounds[1] + 1)) # For each column for i in range(len(labels)): - allvalid = True - for ticket in vtickets: - if ticket[i] not in valid: - allvalid = False - break - - if allvalid == True: + if all(ticket[i] in valid for ticket in vtickets): label_dict[i].append(label) # Reduce the label dictionary |