Solve day3 part 2

This commit is contained in:
Gabriel Augendre 2022-12-03 22:15:10 +01:00
parent 2e029f2b8f
commit 4acef023c2

View file

@ -7,8 +7,8 @@ def main(filename: str, expected_part_1: int = None, expected_part_2: int = None
with open(filename) as f: with open(filename) as f:
data = f.read().strip().split("\n") data = f.read().strip().split("\n")
data = parse_data(data) data_1 = parse_data(data)
solution_part_1 = solve_part_1(data) solution_part_1 = solve_part_1(data_1)
print(f"1. Found {solution_part_1}") print(f"1. Found {solution_part_1}")
if expected_part_1: if expected_part_1:
@ -41,10 +41,19 @@ def solve_part_1(data: DataType) -> int:
return total return total
def solve_part_2(data: DataType) -> int: def solve_part_2(data: List[str]) -> int:
return 0 total = 0
for chunk in chunks(data, 3):
common_letter = set(chunk[0]).intersection(set(chunk[1])).intersection(set(chunk[2])).pop()
total += string.ascii_letters.index(common_letter) + 1
return total
def chunks(data, size):
for i in range(0, len(data), size):
yield data[i:i+size]
if __name__ == "__main__": if __name__ == "__main__":
main("inputs/day03-test1", expected_part_1=157)
main("inputs/day03", expected_part_1=7716) main("inputs/day03-test1", expected_part_1=157, expected_part_2=70)
main("inputs/day03", expected_part_1=7716, expected_part_2=2973)