aboutsummaryrefslogtreecommitdiff
path: root/2020/14/puzzle-1.py
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
committerThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
commite7c9108b95e39d7ea5a29ae06d619c4727f11027 (patch)
tree237261eef3afd0720be77dbcbb9599fa66a24b67 /2020/14/puzzle-1.py
Initial commit
Diffstat (limited to '2020/14/puzzle-1.py')
-rwxr-xr-x2020/14/puzzle-1.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/2020/14/puzzle-1.py b/2020/14/puzzle-1.py
new file mode 100755
index 0000000..f102316
--- /dev/null
+++ b/2020/14/puzzle-1.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+
+def bitmask(mask: str, num: int) -> int:
+ binary = bin(num)[2:]
+ binary = "0" * (36 - len(binary)) + binary
+ res = ""
+
+ for i in range(len(binary)):
+ if binary[i] == "1" and mask[i] == "0":
+ res += "0"
+ elif binary[i] == "0" and mask[i] == "1":
+ res += "1"
+ else:
+ res += binary[i]
+
+ return int(res, 2)
+
+
+def main() -> None:
+ with open("input", "r") 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]))
+
+ print(sum(mem[val] for val in mem))
+
+
+if __name__ == "__main__":
+ main()