retyped day2, completed days 3 and 4, modified template
This commit is contained in:
38
day3/part2.py
Normal file
38
day3/part2.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from string import ascii_lowercase
|
||||
|
||||
|
||||
def parse(filename: str) -> list[tuple[str, str, str]]:
|
||||
groups = []
|
||||
i = 0
|
||||
group = []
|
||||
with open(filename) as f:
|
||||
for line in f.readlines():
|
||||
if i % 3 == 0 and i != 0:
|
||||
groups.append(group)
|
||||
group = []
|
||||
group.append(line[:-1])
|
||||
i += 1
|
||||
groups.append(group)
|
||||
return tuple(groups)
|
||||
|
||||
|
||||
def convert(char: str) -> int:
|
||||
return ord(char) - 96 if char in ascii_lowercase else ord(char) - 38
|
||||
|
||||
|
||||
def common_character(group: tuple[str, str, str]) -> str:
|
||||
assert len(group) == 3
|
||||
b1, b2, b3 = group
|
||||
return filter(lambda c: c in b2 and c in b3, b1).__next__()
|
||||
|
||||
|
||||
def solve(groups: list[tuple[str, str, str]]) -> int:
|
||||
return sum(map(convert, map(common_character, groups)))
|
||||
|
||||
|
||||
def main():
|
||||
print(solve(parse("input")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user