retyped day2, completed days 3 and 4, modified template
This commit is contained in:
1
day3/common.py
Normal file
1
day3/common.py
Normal file
@@ -0,0 +1 @@
|
||||
# Nothing in common
|
||||
22
day3/part1.py
Normal file
22
day3/part1.py
Normal 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()
|
||||
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()
|
||||
6
day3/toy_input
Normal file
6
day3/toy_input
Normal file
@@ -0,0 +1,6 @@
|
||||
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||
PmmdzqPrVvPwwTWBwg
|
||||
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||
ttgJtRGJQctTZtZT
|
||||
CrZsJsPPZsGzwwsLwLmpwMDw
|
||||
Reference in New Issue
Block a user