mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-22 05:41:51 +01:00
Solve day4
This commit is contained in:
parent
c04ee4c040
commit
ae62ed7615
3 changed files with 1060 additions and 0 deletions
54
2022/day04_camp_cleanup.py
Normal file
54
2022/day04_camp_cleanup.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
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")
|
||||
|
||||
data = parse_data(data)
|
||||
solution_part_1 = solve_part_1(data)
|
||||
|
||||
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
|
||||
|
||||
|
||||
DataType = tuple[set[int], set[int]]
|
||||
|
||||
|
||||
def parse_data(data: list[str]) -> DataType:
|
||||
lines = []
|
||||
for line in data:
|
||||
first, second = line.split(",")
|
||||
first_1, first_2 = first.split("-")
|
||||
first = set(range(int(first_1), int(first_2)+1))
|
||||
second_1, second_2 = second.split("-")
|
||||
second = set(range(int(second_1), int(second_2)+1))
|
||||
lines.append((first, second))
|
||||
return lines
|
||||
|
||||
|
||||
def solve_part_1(data: DataType) -> int:
|
||||
total = 0
|
||||
for line in data:
|
||||
first, second = line
|
||||
if first.issuperset(second) or second.issuperset(first):
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
def solve_part_2(data: DataType) -> int:
|
||||
total = 0
|
||||
for line in data:
|
||||
first, second = line
|
||||
if first.intersection(second) != set():
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main("inputs/day04-test1", expected_part_1=2, expected_part_2=4)
|
||||
main("inputs/day04", expected_part_1=413)
|
1000
2022/inputs/day04
Normal file
1000
2022/inputs/day04
Normal file
File diff suppressed because it is too large
Load diff
6
2022/inputs/day04-test1
Normal file
6
2022/inputs/day04-test1
Normal file
|
@ -0,0 +1,6 @@
|
|||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
Loading…
Reference in a new issue