Files
advent-of-code/2022/03/main.py

44 lines
1.1 KiB
Python

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)