bob
Bob 2025-12-05 17:35:24 +01:00
parent e7805f5169
commit accbdf81a9
5 changed files with 1307 additions and 0 deletions

41
bob/day05/day05-p1.py Normal file
View File

@ -0,0 +1,41 @@
def item_in_range(item, id_range):
start = id_range[0]
end = id_range[1]
return start <= item <= end
def check_item(item, id_ranges):
for id_range in id_ranges:
if item_in_range(item, id_range):
return True
return False
def read_input(filename):
id_ranges = []
inventory = []
read_range = True
for line in open(filename).read().splitlines():
if line == "":
read_range = False
continue
if read_range:
id_range = line.split("-")
id_ranges.append((int(id_range[0]),int(id_range[1])))
else:
inventory.append(int(line))
return id_ranges, inventory
# filename = "day05/example_input" # 3
filename = "day05/input" # 679
id_ranges, inventory = read_input(filename)
counter = 0
for item in inventory:
if check_item(item, id_ranges):
counter += 1
print(f"Fresh items: {counter}")

60
bob/day05/day05-p2.py Normal file
View File

@ -0,0 +1,60 @@
def item_in_range(item, id_range):
start = id_range[0]
end = id_range[1]
return start <= item <= end
def squash_ranges(id_ranges):
while True:
id_ranges_size = len(id_ranges)
squash_range = []
squash_range.append(id_ranges[0].copy())
for checker in id_ranges:
overlap = False
for candidate in squash_range:
# Squash if checker min within candidate range or otherway around
if item_in_range(checker[0], candidate) or item_in_range(candidate[0], checker):
overlap = True
candidate[0] = min(checker[0],candidate[0])
candidate[1] = max(checker[1],candidate[1])
if not overlap:
squash_range.append(checker.copy())
id_ranges = squash_range.copy()
if id_ranges_size == len(id_ranges):
# If no changes, break
break
return id_ranges
def read_input(filename):
id_ranges = []
for line in open(filename).read().splitlines():
if line == "":
return id_ranges
id_range = line.split("-")
id_ranges.append([int(id_range[0]),int(id_range[1])])
# filename = "day05/my_example_input"
# filename = "day05/example_input" # 14
filename = "day05/input" # 358155203664116
id_ranges = read_input(filename)
id_ranges = squash_ranges(id_ranges)
counter = 0
for id_range in id_ranges:
counter += (id_range[1] - id_range[0]) + 1 # ranges are inclusive...
print(f"Fresh items: {counter}")

11
bob/day05/example_input Normal file
View File

@ -0,0 +1,11 @@
3-5
10-14
16-20
12-18
1
5
8
11
17
32

1183
bob/day05/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
17-19
3-5
10-14
16-20
12-18
1
5
8
11
17
32