aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-02 13:58:27 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-02 13:58:27 +0100
commit3ac99f409ae6545e2f4fece4d9397d28d35b279b (patch)
tree5757e252e197e97d6f64f5485a07a8c83739e20c /2020
parente257eb53359927c537d2e9c31ca8eae7a0f6dfe7 (diff)
Set file encoding when reading
Diffstat (limited to '2020')
-rwxr-xr-x2020/07/puzzle-1.py14
-rwxr-xr-x2020/07/puzzle-2.py4
-rwxr-xr-x2020/09/puzzle-1.py11
-rwxr-xr-x2020/09/puzzle-2.py3
-rwxr-xr-x2020/10/puzzle-2.py16
-rwxr-xr-x2020/13/puzzle-1.py2
-rwxr-xr-x2020/13/puzzle-2.py1
-rwxr-xr-x2020/14/puzzle-1.py14
-rwxr-xr-x2020/14/puzzle-2.py22
-rwxr-xr-x2020/16/puzzle-1.py8
-rwxr-xr-x2020/16/puzzle-2.py33
11 files changed, 46 insertions, 82 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:
diff --git a/2020/09/puzzle-1.py b/2020/09/puzzle-1.py
index f68fc21..7676f2d 100755
--- a/2020/09/puzzle-1.py
+++ b/2020/09/puzzle-1.py
@@ -1,17 +1,12 @@
#!/usr/bin/env python3
-from typing import List
-def is_valid(nums: List[int], lp: int, up: int) -> bool:
- for i in range(lp, up):
- x = nums[up] - nums[i]
- if x in nums[lp:up] and x != nums[i]:
- return True
- return False
+def is_valid(nums: list[int], lp: int, up: int) -> bool:
+ return any(((x := nums[up] - nums[i]) in nums[lp:up] and x != nums[i]) for i in range(lp, up))
def main() -> None:
- with open("input", "r") as f:
+ with open("input", "r", encoding="utf-8") as f:
nums = list(map(int, f.readlines()))
lp = 0
diff --git a/2020/09/puzzle-2.py b/2020/09/puzzle-2.py
index 8833f50..c890fb3 100755
--- a/2020/09/puzzle-2.py
+++ b/2020/09/puzzle-2.py
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
+
import numpy as np
def main() -> None:
goal = 138879426
- with open("input", "r") as f:
+ with open("input", "r", encoding="utf-8") as f:
nums = np.array(list(map(int, f.readlines())), dtype=int)
lp = 0
diff --git a/2020/10/puzzle-2.py b/2020/10/puzzle-2.py
index bdf7bbf..3042a2b 100755
--- a/2020/10/puzzle-2.py
+++ b/2020/10/puzzle-2.py
@@ -1,7 +1,5 @@
#!/usr/bin/env python3
-from typing import List
-
adaptors: list[str]
@@ -11,18 +9,16 @@ def combos(i: int, counts: dict[int, dict[int, int]] = {}) -> int:
if i in counts:
return counts[i]
- ans = 0
- for j in range(i + 1, len(adaptors)):
- if adaptors[j] - adaptors[i] <= 3:
- ans += combos(j, counts)
-
- counts[i] = ans
- return ans
+ counts[i] = sum(
+ combos(j, counts) if adaptors[j] - adaptors[i] <= 3 else 0
+ for j in range(i + 1, len(adaptors))
+ )
+ return counts[i]
def main() -> None:
global adaptors
- with open("input", "r") as f:
+ with open("input", "r", encoding="utf-8") as f:
adaptors = list(map(int, f.readlines()))
adaptors.append(0)
diff --git a/2020/13/puzzle-1.py b/2020/13/puzzle-1.py
index 7b21f96..ef83817 100755
--- a/2020/13/puzzle-1.py
+++ b/2020/13/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:
time = int(f.readline())
ids = list(map(int, f.readline().replace(",x", "").split(",")))
diff --git a/2020/13/puzzle-2.py b/2020/13/puzzle-2.py
index 3965488..cf16c22 100755
--- a/2020/13/puzzle-2.py
+++ b/2020/13/puzzle-2.py
@@ -32,7 +32,6 @@ def main() -> None:
buses = tuple(filter(lambda x: x != "x", ids))
modres = tuple(x if (x := bus - ids.index(bus)) != bus else 0 for bus in buses)
-
print(chinese_remainder(buses, modres))
diff --git a/2020/14/puzzle-1.py b/2020/14/puzzle-1.py
index f102316..81f9136 100755
--- a/2020/14/puzzle-1.py
+++ b/2020/14/puzzle-1.py
@@ -18,19 +18,19 @@ def bitmask(mask: str, num: int) -> int:
def main() -> None:
- with open("input", "r") as f:
+ with open("input", "r", encoding="utf-8") as f:
lines = f.read().splitlines()
mem: dict[str, int] = {}
mask = ""
for line in lines:
- line = line.split(" ")
- if line[0] == "mask":
- mask = line[2]
- else:
- mem[line[0][4:-1]] = bitmask(mask, int(line[2]))
+ match line.split(" "):
+ case ["mask", _, mask]:
+ mask = mask
+ case [addr, _, val]:
+ mem[addr[4:-1]] = bitmask(mask, int(val))
- print(sum(mem[val] for val in mem))
+ print(sum(mem.values()))
if __name__ == "__main__":
diff --git a/2020/14/puzzle-2.py b/2020/14/puzzle-2.py
index 45fb6ad..726da0a 100755
--- a/2020/14/puzzle-2.py
+++ b/2020/14/puzzle-2.py
@@ -26,22 +26,22 @@ def bitmask(mask: str, num: int) -> tuple[int, ...]:
def main() -> None:
- with open("input", "r") as f:
+ with open("input", "r", encoding="utf-8") as f:
lines = f.read().splitlines()
mem: dict[str, int] = {}
mask = ""
for line in lines:
- line = line.split(" ")
- if line[0] == "mask":
- mask = line[2]
- else:
- address = line[0][4:-1]
- addresses = bitmask(mask, int(address))
- for a in addresses:
- mem[a] = int(line[2])
-
- print(sum(mem[val] for val in mem))
+ match line.split(" "):
+ case ["mask", _, mask]:
+ mask = mask
+ case [addr, _, val]:
+ address = addr[4:-1]
+ addresses = bitmask(mask, int(address))
+ for a in addresses:
+ mem[a] = int(val)
+
+ print(sum(mem.values()))
if __name__ == "__main__":
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