2025(05): part 1 is freshly done
This commit is contained in:
@ -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?_
|
||||
|
||||
33
2025/05/part1.py
Normal file
33
2025/05/part1.py
Normal file
@ -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))
|
||||
Reference in New Issue
Block a user