From e0a607a69021e6f23c74805df9bdfd6bf4dd60cb Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Fri, 5 Dec 2025 10:12:34 +0100 Subject: [PATCH] 2025(05): part 1 is freshly done --- 2025/05/README.md | 18 ++++++++++++++++++ 2025/05/part1.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 2025/05/part1.py diff --git a/2025/05/README.md b/2025/05/README.md index 3e6feab..708b73d 100644 --- a/2025/05/README.md +++ b/2025/05/README.md @@ -43,3 +43,21 @@ The Elves are trying to determine which of the _available ingredient IDs_ are _f 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?_ diff --git a/2025/05/part1.py b/2025/05/part1.py new file mode 100644 index 0000000..e2740ac --- /dev/null +++ b/2025/05/part1.py @@ -0,0 +1,33 @@ +import numpy as np + +def read_inventory(filename: str): + # Read file as list of strings + with open(filename, "r") as fp: + data = fp.read().splitlines() + + # Split on the empty line to separate ranges and items + splitpoint = data.index("") + ranges, items = data[:splitpoint], data[splitpoint + 1:] + + return np.array([ran.split("-") for ran in ranges], dtype=int), np.array(items, dtype=int) + +def is_fresh(ranges, item): + mins = ranges[:,0] + maxs = ranges[:,1] + return np.any((mins <= item)*(item <= maxs)) + +def count_fresh_items(ranges, items): + return sum([is_fresh(ranges, item) for item in items]) + +if __name__ == "__main__": + test_ranges, test_items = read_inventory("testinput") + # for item in test_items: + # print(is_fresh(item, test_ranges)) + assert count_fresh_items(test_ranges, test_items) == 3 + + test_freshes = [False, True, False, True, True, False] + for idx, item in enumerate(test_items): + assert is_fresh(test_ranges, item) == test_freshes[idx] + + ranges, items = read_inventory("input") + print(count_fresh_items(ranges, items)) \ No newline at end of file