advent-of-code/2022/day03_rucksack.py

61 lines
1.6 KiB
Python
Raw Permalink Normal View History

2022-12-03 09:26:37 +01:00
import string
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
print(f"\n+ Running on {filename}")
with open(filename) as f:
data = f.read().strip().split("\n")
2022-12-03 22:15:10 +01:00
data_1 = parse_data(data)
solution_part_1 = solve_part_1(data_1)
2022-12-03 09:26:37 +01:00
print(f"1. Found {solution_part_1}")
if expected_part_1:
assert expected_part_1 == solution_part_1
solution_part_2 = solve_part_2(data)
print(f"2. Found {solution_part_2}")
if expected_part_2:
assert expected_part_2 == solution_part_2
2022-12-03 22:24:17 +01:00
DataType = list[tuple[str, str]]
2022-12-03 09:26:37 +01:00
2022-12-03 22:24:17 +01:00
def parse_data(data: list[str]) -> DataType:
2022-12-03 09:26:37 +01:00
sacks = []
for line in data:
2022-12-03 22:30:29 +01:00
stop = int(len(line) / 2)
2022-12-03 09:26:37 +01:00
halves = (line[:stop], line[stop:])
sacks.append(halves)
return sacks
def solve_part_1(data: DataType) -> int:
total = 0
for halves in data:
first_half, second_half = set(halves[0]), set(halves[1])
common_letter = first_half.intersection(second_half).pop()
total += string.ascii_letters.index(common_letter) + 1
return total
2022-12-03 22:24:17 +01:00
def solve_part_2(data: list[str]) -> int:
2022-12-03 22:15:10 +01:00
total = 0
for chunk in chunks(data, 3):
2022-12-03 22:30:29 +01:00
common_letter = (
set(chunk[0]).intersection(set(chunk[1])).intersection(set(chunk[2])).pop()
)
2022-12-03 22:15:10 +01:00
total += string.ascii_letters.index(common_letter) + 1
return total
2022-12-03 09:26:37 +01:00
2022-12-03 22:15:10 +01:00
def chunks(data, size):
for i in range(0, len(data), size):
2022-12-03 22:30:29 +01:00
yield data[i : i + size]
2022-12-03 22:15:10 +01:00
2022-12-03 09:26:37 +01:00
if __name__ == "__main__":
2022-12-03 22:15:10 +01:00
main("inputs/day03-test1", expected_part_1=157, expected_part_2=70)
main("inputs/day03", expected_part_1=7716, expected_part_2=2973)