(update) 2022 day 3 part 1

This commit is contained in:
2025-12-24 22:36:50 +01:00
parent c5a6b4a82d
commit 78d1a4a28c
3 changed files with 349 additions and 0 deletions

43
2022/03/main.py Normal file
View File

@ -0,0 +1,43 @@
print("a", ord("a"), "A", ord("A"))
def getitems(items: list[str]) -> int:
# Source - https://stackoverflow.com/a
# Posted by georg, modified by community. See post 'Timeline' for change history
# Retrieved 2025-12-24, License - CC BY-SA 4.0
seen = set()
dupes = [x for x in items if x in seen or seen.add(x)]
print(dupes)
return sum(list(map(lambda x: ord(x), dupes)))
def tr(char: str) -> str:
return char.upper() if char.islower() else char.lower()
def gitems(items: list[str]) -> int:
# print(items, len(items))
n = int(len(items) / 2)
l, r = [(items[i : i + n]) for i in range(0, len(items), n)]
# seen = set()
# dupes = [x for x in items if x in seen or seen.add(x)]
dupes = set(l) & set(r)
rv = sum(
list(map(lambda x: ord(tr(x)) - 64 if x.islower() else ord(tr(x)) - 70, dupes))
)
print(items, dupes, rv)
return rv
with open("input", "+r") as file:
t = 0
for line in file.readlines():
that = gitems(line.strip())
t += that
# print(line.strip(), that)
print(t)