Completed submission for day 05!

This commit is contained in:
Ada Werefox 2025-12-05 11:37:46 -08:00
parent 5c4b07cc3f
commit 99ec95aa03
4 changed files with 1316 additions and 0 deletions

55
05/README.md Normal file
View file

@ -0,0 +1,55 @@
# --- Day 5: Cafeteria ---
As the forklifts break through the wall, the Elves are delighted to discover that there was a cafeteria on the other side after all.
You can hear a commotion coming from the kitchen. "At this rate, we won't have any time left to put the wreaths up in the dining hall!" Resolute in your quest, you investigate.
"If only we hadn't switched to the new inventory management system right before Christmas!" another Elf exclaims. You ask what's going on.
The Elves in the kitchen explain the situation: because of their complicated new inventory management system, they can't figure out which of their ingredients are fresh and which are spoiled. When you ask how it works, they give you a copy of their database (your puzzle input).
The database operates on ingredient IDs. It consists of a list of fresh ingredient ID ranges, a blank line, and a list of available ingredient IDs. For example:
3-5
10-14
16-20
12-18
1
5
8
11
17
32
The fresh ID ranges are inclusive: the range 3-5 means that ingredient IDs 3, 4, and 5 are all fresh. The ranges can also overlap; an ingredient ID is fresh if it is in any range.
The Elves are trying to determine which of the available ingredient IDs are fresh. In this example, this is done as follows:
Ingredient ID 1 is spoiled because it does not fall into any range.
Ingredient ID 5 is fresh because it falls into range 3-5.
Ingredient ID 8 is spoiled.
Ingredient ID 11 is fresh because it falls into range 10-14.
Ingredient ID 17 is fresh because it falls into range 16-20 as well as range 12-18.
Ingredient ID 32 is spoiled.
So, in this example, 3 of the available ingredient IDs are fresh.
Process the database file from the new inventory management system. How many of the available ingredient IDs are fresh?
## --- Part Two ---
The Elves start bringing their spoiled inventory to the trash chute at the back of the kitchen.
So that they can stop bugging you when they get new inventory, the Elves would like to know all of the IDs that the fresh ingredient ID ranges consider to be fresh. An ingredient ID is still considered fresh if it is in any range.
Now, the second section of the database (the available ingredient IDs) is irrelevant. Here are the fresh ingredient ID ranges from the above example:
3-5
10-14
16-20
12-18
The ingredient IDs that these ranges consider to be fresh are 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. So, in this example, the fresh ingredient ID ranges consider a total of 14 ingredient IDs to be fresh.
Process the database file again. How many ingredient IDs are considered to be fresh according to the fresh ingredient ID ranges?

1168
05/input/ingredients.txt Normal file

File diff suppressed because it is too large Load diff

View file

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

82
05/main.py Executable file
View file

@ -0,0 +1,82 @@
#!/bin/python
from logging import debug, DEBUG, basicConfig
from sys import argv
def parse_input(input_filepath: str) -> tuple[list[tuple[int, int]], list[int]]:
output: tuple[list[tuple[int, int]], list[int]] = ([], [])
with open(file=input_filepath, mode="r") as input_file:
input_data: list[str] = input_file.readlines()
debug(f"\n\nRAW INPUT: {input_data}\n\n")
ingredient_id = False
for line in input_data:
if line.strip() == "":
ingredient_id = True
continue
if ingredient_id:
output[1].append(int(line.strip()))
continue
output[0].append(
(int(line.strip().split("-")[0]), int(line.strip().split("-")[1]))
)
return output
def find_available_fresh_ids(
fresh_id_ranges: list[tuple[int, int]], available_ids: list[int]
) -> int:
available_fresh_ids = 0
for id in available_ids:
for id_range in fresh_id_ranges:
if id >= id_range[0] and id <= id_range[1]:
available_fresh_ids += 1
break
return available_fresh_ids
def condense_id_ranges(id_ranges: list[tuple[int, int]]) -> list[tuple[int, int]]:
id_ranges.sort(key=lambda id: id[0])
debug(f"SORTED ID RANGES: {id_ranges}")
condensed_ranges: list[tuple[int, int]] = []
temp_range: tuple[int, int] = id_ranges[0]
for i in range(1, len(id_ranges)):
if temp_range[1] > id_ranges[i][1]:
continue
if temp_range[1] < id_ranges[i][0]:
condensed_ranges.append(temp_range)
temp_range = id_ranges[i]
continue
temp_range = (temp_range[0], id_ranges[i][1])
condensed_ranges.append(temp_range)
return condensed_ranges
def main() -> None:
input_filepath = "input/ingredients.txt"
input_fresh_ranges, input_available_ids = parse_input(input_filepath)
debug(f"INPUT RANGES: {input_fresh_ranges}")
debug(f"INPUT AVAILABLE IDS: {input_available_ids}")
available_fresh_ids = find_available_fresh_ids(
input_fresh_ranges, input_available_ids
)
debug(f"AVAILABLE FRESH IDS: {available_fresh_ids}")
print(f"There are {available_fresh_ids} available fresh ingedients.")
condensed_id_ranges = condense_id_ranges(input_fresh_ranges)
debug(f"CONDENSESD RANGES: {condensed_id_ranges}")
sum_of_id_ranges = sum([x[1] - x[0] + 1 for x in condensed_id_ranges])
print(f"There are {sum_of_id_ranges} total fresh ingredient IDs.")
return
if __name__ == "__main__":
if "-d" in argv or "--debug" in argv:
basicConfig(filename="debug.log", level=DEBUG)
main()
exit(0)