AoC 2020 D16: Ticket Translation
| Problem statement | Source code | Tags: Constraint satisfaction
← Previous Back to AoC Index Next →
Part 1
Part 1 is straightforward: collect a list of ranges, then for each number in each nearby ticket, check if it falls into any of the ranges.
Part 2
We have a list of sets of values, and we want to find an assignment of the fields such that each set only contains values from the field's range. The first step is to check which fields are possible for each set. For each set, we iterate through all fields and check if all values in the set fall into the range.
Like in any constraint satisfaction problem, once we have the initial candidates, we can propagate constraints. We start with the narrowest constraints, satisfy them, and remove that possibility from all other sets. We repeat this until all sets are satisfied.
If we print candidate_fields, it looks like this:
It turns out that this problem is very nice: it doesn't even need backtracking. Each step is always deterministic because there's always a constraint with only one possibility. So the field assignments can be found with a simple loop, by sorting the candidate sets by size and removing already-assigned fields from subsequent sets.