day16: faster and more readable
This commit is contained in:
parent
2d6dd0e074
commit
66ad0f3f70
6
day16.py
6
day16.py
@ -20,14 +20,14 @@ def parse_input(to_parse):
|
|||||||
limit_low, limit_hi = limits.split(" or ")
|
limit_low, limit_hi = limits.split(" or ")
|
||||||
min_low, max_low = limit_low.split("-")
|
min_low, max_low = limit_low.split("-")
|
||||||
min_hi, max_hi = limit_hi.split("-")
|
min_hi, max_hi = limit_hi.split("-")
|
||||||
restraints[field] = [(int(min_low), int(max_low)), (int(min_hi), int(max_hi))]
|
restraints[field] = set(range(int(min_low), int(max_low) + 1)) | set(range(int(min_hi), int(max_hi) + 1))
|
||||||
|
|
||||||
return restraints, my_ticket, other_tickets
|
return restraints, my_ticket, other_tickets
|
||||||
|
|
||||||
|
|
||||||
def is_valid_number(number, restraints):
|
def is_valid_number(number, restraints):
|
||||||
for restraint in restraints.values():
|
for restraint in restraints.values():
|
||||||
if restraint[0][0] <= number <= restraint[0][1] or restraint[1][0] <= number <= restraint[1][1]:
|
if number in restraint:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -87,7 +87,7 @@ def part2(test_mode=False):
|
|||||||
|
|
||||||
for position, number in enumerate(ticket):
|
for position, number in enumerate(ticket):
|
||||||
for field, restraint in restraints.items():
|
for field, restraint in restraints.items():
|
||||||
if not restraint[0][0] <= number <= restraint[0][1] and not restraint[1][0] <= number <= restraint[1][1]:
|
if number not in restraint:
|
||||||
possible_positions[field].remove(position)
|
possible_positions[field].remove(position)
|
||||||
|
|
||||||
possible_positions = get_single_possible_positions(possible_positions)
|
possible_positions = get_single_possible_positions(possible_positions)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user