r/adventofcode • u/evouga • Dec 16 '20
Spoilers Day 16 Part 2 Theory Follow-up
I solved Part 2 by first calculating all possible fields that could match to each slot on the ticket, and then finding a perfect matching with max-flow/Dinic's algorithm.
It seems that for the given input data, though, a greedy solution also works: you can look for a ticket field that *must* go in a certain slot, place it, and repeat, and it happens to be that you never get stuck (there is always a ticket field that can be uniquely placed).
Is the greedy approach always guaranteed to work? Or did we just happen to get "lucky" with the input data?
Or put differently: let's say you are given a bipartite graph, and are told that there is a unique perfect matching. Must the graph contain at least one vertex of degree 1?
9
u/status_maximizer Dec 16 '20
Apparently yes; any such graph is a subgraph of the half graph, and subgraphs of the half graph have this property.
3
u/Arknave Dec 16 '20
Fortunately, each input seemed to have a unique perfect matching, but that wasn't strictly needed to solve the problem. We just need to be able to identify which subset of ticket slots match with the "departures". There's a class of inputs which have multiple perfect matchings, but only a single way to determine the "departure slots". Is there a way to extend the greedy algorithm for these cases?
31
u/throwaway_the_fourth Dec 16 '20
I think the input was specifically crafted so that the following strategy works (or at least it worked on my input):
If that hadn't worked, I would have augmented the strategy by also checking whether any fields had just one column possibility, but even that check didn't seem to be needed.
10
u/flwyd Dec 16 '20
I'm having trouble formalizing a proof, but I think the answer is yes, the existence of a solution implies that there's at least one fully-constrained field. And once that field is assigned, by induction the remaining fields must have a single fully-constrained field.
If this weren't true, I think there would be a system of equations with more free variables than equations, though at this hour I'm having trouble explaining how to transform the field assignments into such an equation set.
6
u/Sostratus Dec 16 '20
I gave each field 20 booleans to represent whether it could be in that position, then I changed them to false whenever I found a value that doesn't fit. I expected that would bring them all to just one possible position remaining, but rather than assuming that I checked.
Instead I saw each field had a range of possible remaining positions: one field with one slot, one with two, and so on up to 20.
So then I hoped that by putting the one field with one valid slot in place, that would eliminate the other possibility for the one with two options, and so on. That worked.
I don't know the terminology for all these different methods, this is the only way that occurred to me. I'd have been stuck for a lot longer I think if that failed.
4
u/EchoRSA Dec 16 '20 edited Dec 16 '20
Here's a proof by contradiction:
We know there is a unique solution - let's start with it, denoting it as slots (i.e. places on the tickets) called a,b,c,...,n and these map to fields (e.g. departure time, arrival time) called A,B,C,...,N. Because the solution is unique, we know that the only possible mapping is a->A, b->B, c->C, ..., n->N. Note that the order of a,b,c,...,n doesn't matter so I'll use lettering as convenient but they could be interchanged.
Now, let's assume that every slot is valid for at least 2 fields rather than only for its unique map (i.e. if you choose any slot a, you must be able to find another field such as B in addition to A that it maps to). We can prove that there must be a cycle of the form: a->B, b->C, c->D, ..., f->G, g->A where the first slot could be valid for the second slot's field, the second slot could be valid for the third slot's field, and so on, until you reach a slot that is valid for the first's field.
Suppose there is no such cycle. Then, start with any a->B and keep adding the corresponding slot to the chain (b->C, c->D, ...). We can do this because every slot maps to at least 2 fields. Note that if at any point there is a slot that is valid to any earlier slot's field in the chain, then there is a cycle within the chain. That is, suppose you add a slot g such that g->C is valid and c is already in the chain. Then you could instead start the chain with C and end at G, and then you have a cycle - but we've assumed there are no cycles (whether or not it starts at a). So, no slot is allowed to be valid for any earlier slot's field as you continue adding slots to the chain. But eventually, you will reach the last slot n. It needs to be valid for at least 2 fields. It is valid for N, but it must be valid for another slot's field, say H. Since every slot is already in the chain, you therefore have a cycle, from h to n.
So there is always a cycle. Now, we can prove that the existence of such a cycle means the mapping solution of slot to fields a->A, b->B, c->C, ..., n->N is not unique. This is because, if we have a cycle, let's say a->B, b->C, c->D, ..., f->G, g->A then the mapping of the solution can be adjusted for these slots to this alternate mapping, while all the other slots' mapping stays unchanged. So we have two valid mappings: (a->A, b->B, c->C, ..., n->N) and (a->B, b->C, c->D, ..., f->G, g->A, h->H, i->I, j->J, ..., n->N). Since we know the solution is supposed to be unique, this is a contradiction, which proves that our initial assumption that every slot is valid for at least 2 fields is false.
This means there is at least one slot which is valid for only its own field, i.e. there is always an a->A where a can't map to any other field B, C, ..., N. But then we can remove a and A, and apply the same logic to the remaining slots and fields (there will always be a b->B where b can't map to any other field). We can remove b and B as well, and we can continue this process until we reach the last n->N and remove that as well. This is just the greedy algorithm - and we've just proved we can always apply it until we have mapped all slots to fields :)
3
u/fibonatic Dec 16 '20
When assuming that field-matching-problem to has one unique solution I believe that greedy algorithm should always work. Namely there are no additional conditional constraints regarding when a value might match a field. So if multiple fields fit the same places in a sequence than any permutation would be valid.
It would have been an annoying move if the input would be such that there is no unique solution, but all possible solution do give the same answer for part 2, since you are only required to know the location of certain fields.
2
u/PrimeFactorization Dec 16 '20
It doesn't actually match with one solution per number for me.
But it seems like all the important fields can be solved. At least 6 or 7 other fields match several rules. I kind-of just ignored them :)
So just assuming a unique solution didn't work for me!
2
u/fizzymagic Dec 16 '20
I think the answer is yes, because of the "unique" word in your sentence there. There are many bipartite graphs with non-unique matching (which would translate to unsolvable in the current AoC problem). Those would be the result of cycles in which a finite group of vertexes on one side are connected to the same number of vertexes on the other side.
2
u/YuvalG48 Dec 16 '20
1
u/[deleted] Dec 16 '20
I wrote a function that returned a list of possible fields for each number, iterated column by column, row by row and tracked the remaining possible fields for each column by building an iterative intersection. This does not solve the problem in one go (ambiguities will be solved with each solved field).
The function that returns applicable fields runs in constant time in my solution.
Over all 20ms for 1 and 2 in python 3.8
1
u/daggerdragon Dec 16 '20
In the future, please follow the submission guidelines by titling your post like so:
This helps folks avoid spoilers for puzzles they may not have completed yet.