retyped day2, completed days 3 and 4, modified template

This commit is contained in:
2024-12-01 16:19:37 +01:00
parent 42bab0cb9e
commit 9e21d8d9c8
12 changed files with 134 additions and 26 deletions

22
day3/part1.py Normal file
View File

@@ -0,0 +1,22 @@
from string import ascii_lowercase
def parse(filename: str) -> list[tuple[str, str]]:
with open(filename) as f:
return list(map(lambda x: (x[0 : int((len(x) - 1) / 2)], x[int((len(x) - 1) / 2) : -1]), f.readlines()))
def convert(char: str) -> int:
return ord(char) - 96 if char in ascii_lowercase else ord(char) - 38
def solve(bagpacks: list[tuple[str, str]]) -> int:
return sum(map(convert, [(filter(lambda x: x in bagpack[1], bagpack[0])).__next__() for bagpack in bagpacks]))
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()