diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-02 13:58:27 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-02 13:58:27 +0100 |
commit | 3ac99f409ae6545e2f4fece4d9397d28d35b279b (patch) | |
tree | 5757e252e197e97d6f64f5485a07a8c83739e20c /2020/07 | |
parent | e257eb53359927c537d2e9c31ca8eae7a0f6dfe7 (diff) |
Set file encoding when reading
Diffstat (limited to '2020/07')
-rwxr-xr-x | 2020/07/puzzle-1.py | 14 | ||||
-rwxr-xr-x | 2020/07/puzzle-2.py | 4 |
2 files changed, 5 insertions, 13 deletions
diff --git a/2020/07/puzzle-1.py b/2020/07/puzzle-1.py index da054b8..3c0d0e3 100755 --- a/2020/07/puzzle-1.py +++ b/2020/07/puzzle-1.py @@ -9,15 +9,12 @@ def holds_bag(innerbags: list[str]) -> bool: return False elif "shiny gold" in innerbags: return True - - for subbag in innerbags: - if holds_bag(bdict[subbag]): - return True + return any(holds_bag(bdict[subbag]) for subbag in innerbags) def main() -> None: global bdict - with open("input", "r") as f: + with open("input", "r", encoding="utf-8") as f: lines = f.readlines() for baginfo in lines: @@ -26,12 +23,7 @@ def main() -> None: # { bag_name: [contained_bag_1, containted_bag_2, ...] } bdict[data[0]] = [" ".join(b.split(" ")[-3:][:2]) for b in data[1].split(",")] - count = 0 - for bag in bdict: - if holds_bag(bdict[bag]): - count += 1 - - print(count) + print(sum(holds_bag(bdict[bag]) for bag in bdict)) if __name__ == "__main__": diff --git a/2020/07/puzzle-2.py b/2020/07/puzzle-2.py index c87ea13..f77298a 100755 --- a/2020/07/puzzle-2.py +++ b/2020/07/puzzle-2.py @@ -8,11 +8,11 @@ def total_bags(innerbags: list[dict[int, str]]) -> int: if innerbags[0]["name"] == "no other": return 0 - return sum([bag["count"] + bag["count"] * total_bags(bdict[bag["name"]]) for bag in innerbags]) + return sum(bag["count"] + bag["count"] * total_bags(bdict[bag["name"]]) for bag in innerbags) def main() -> None: - with open("input", "r") as f: + with open("input", "r", encoding="utf-8") as f: lines = f.readlines() for baginfo in lines: |