Skip to content

Commit

Permalink
Switch to a two-pass system so it works in the general case
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbrew committed Dec 16, 2020
1 parent bfcc4b6 commit 9edd5cd
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Drew/drew_day16.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,26 @@ def part_two(puzzle_input: str) -> int:
if all(number_in_ranges(value, field_rules) for value in field_values):
field_map[field_name].append(field_index)
definite_fields = {}
for field_name, field_index_candidates in field_map.items():
for field_name, field_index_candidates in list(field_map.items()):
if len(field_index_candidates) == 1:
definite_fields[field_name] = field_index_candidates[0]
del field_map[field_name]
result = 1
while field_map:
# NOTE this runs indefinitely on mine. Let's just keep going until we get our 6
# departure fields isolated

# what we're doing is looping through each field to see if we have isolated the candidate
# field indexes down to one value

# first, process singletons
for field_name, field_index_candidates in list(field_map.items()):
if len(field_index_candidates) == 1:
if len(field_index_candidates) == 1:
# yay we've isolated one candidate
definite_fields[field_name] = field_index_candidates[0]
del field_map[field_name]
# then do the rest
for field_name, field_index_candidates in list(field_map.items()):
if field_name in definite_fields:
# we've already matched it. Kill it.
Expand All @@ -115,9 +125,9 @@ def part_two(puzzle_input: str) -> int:
field_index_candidates.remove(value)
except ValueError:
pass
if len(field_index_candidates) == 1:
# yay we've isolated one candidate
definite_fields[field_name] = field_index_candidates[0]
if not field_index_candidates:
raise ValueError(f"No viable candidates for {field_name}")

# by some miracle, have we gotten our 6?
departure_values = list(
val for key, val in definite_fields.items() if key.startswith("departure")
Expand Down

0 comments on commit 9edd5cd

Please # to comment.