9
4
Introduction
There is a tax collector that has some trouble managing the taxes of his kingdom: the historical records have burnt down in a great fire.
He wants to find out how many possible pasts there could be in terms of where the current money was inherited from. Luckily, his kingdom is very simple.
The kingdom can be modelled by a 2D boolean matrix, where l
represents someone who has inherited money, and O
represents someone who has not. For example:
l O l l
O O O l
l O l O
O O O l
(It will always be a rectangle)
In the next generation, the kingdom is smaller (The wolves are strong!).
The next generation would look like this, superimposed on the previous generation (x
is a placeholder for a descendent in the next generation)
l O l l
x x x
O O O l
x x x
l O l O
x x x
O O O l
A descendent will look at the ancestors that are directly around them (So the top-left x
will see {l
, O
, O
, O
}, called an Unaligned rectangular neighbourhood)
If only one ancestor has inherited money, the descendant will inherit money from them. If more than one ancestor has inherited money, they will squabble and the descendant will end up not inheriting money. If no one has inherited money, the descendant will not inherit any money.
(More than one descendant can inherit from one ancestor)
So, the next generation would look like:
l l O
l l O
l l O
Challenge
Input
The current state of the generation, as an array of arrays of any two distinct values, where the inner arrays are all of the same length.
E.g., for the example above, it could be:
[
[True, True, False],
[True, True, False],
[True, True, False]
]
Output
An integer representing the number of unique previous generations where the next generation is the input.
You can assume that the answer will always be less than 2^30 - 1. (or 1073741823).
The previous generation would be called a "preimage" and this challenge would be to count the preimages.
Scoring
This is a fastest-code challenge, so each submission will be tested on my computer, and the submission that takes the least time will be the winner.
Example Input and Output
(Where 1
is a descendent who inherited money, and 0
is a descendent who did not inherit money)
Input:
[[1, 0, 1],
[0, 1, 0],
[1, 0, 1]]
Output:
4
Input:
[[1, 0, 1, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 0, 1, 1, 1]]
Output:
254
Input:
[[1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[1, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 1, 1, 0, 0]]
Output:
11567
6"lOOlLOOOOLLlololoLOLOLOOLOLOLOLL" is all I saw when I first opened the page. – Magic Octopus Urn – 2017-01-23T18:14:24.883