Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Comments on Decoding a non injective bit matrix encoding [FINALIZED]

Post

Decoding a non injective bit matrix encoding [FINALIZED]

+2
−0

Now posted: Decoding a non injective bit matrix encoding


The problem

Someone has created an encoding format for square bit matrices, however they have found it isn't perfect! One encoding may not decode to exactly one matrix, or it may not even be possible to decode.
Knowing this, you're tasked to write a program to decode a set of encodings and since time is of the essence, it's asked that you make it as fast as possible.

The encoding is as follows:

  • an integer, S, indicating the matrix size (2 for 2×2, 3 for 3×3, etc; always ≥2)
  • S integers corresponding to the number of 0s in each line (top-to-bottom)
  • S integers corresponding to the number of 0s in each column (left-to-right)
  • 2 integers corresponding to the number of 0s in each diagonal (main, then anti-diagonal)
  • 4 integers corresponding to the number of 0s in each quadrant (top-right, top-right, bottom-left, bottom-right)
  • S integers corresponding to the number of transitions in each line (top-to-bottom)
  • S integers corresponding to the number of transitions in each column (left-to-right)

The quadrants are defined by the side length divided by two, floored. Here's an example of a 5×5 matrix, with quadrant boundaries highlighted by different digits:

11222
11222
33444
33444
33444

The program should output the number of matrices possible to decode, followed by a representation of such matrices. That representation should be composed of 0s and 1s

Example

Encoding

4
1 1 1 1
1 1 1 1
0 2
0 2 2 0
1 2 2 1
1 2 2 1

Decoded matrix

1
0001
0010
0100
1000

Time constraints

The upper bound for evaluation is 20 seconds to allow everyone to use any language they want.
If you manage to get your decoder to run in under a second for the bigger cases, you will beat me!

Evaluation

All solutions will be evaluated by me on the same machine, and the time measurements posted as a comment on the corresponding answer.
You can expect the latest versions for each language and compiler/interpreter. For Python, PyPy will be used, to make it a more interesting option.

There will be some extra hidden test cases.


More test cases

Input 1

4
2 3 4 3 
2 3 4 3 
2 4
4 3 4 1 
1 1 0 1 
1 1 0 1 

Output 1

0

Input 2

3
0 2 0
1 0 1 
0 0 
0 0 1 1 
0 2 0 
2 0 2 

Output 2

1
000
101
000

Scoreboard

(no submissions yet)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

6 comment threads

Time limit (6 comments)
Input specification (5 comments)
Consistent output (4 comments)
Potential edits (3 comments)
Test cases (3 comments)
Show more
Consistent output
trichoplax‭ wrote almost 2 years ago

To avoid distracting from the main task, it might be better to have the output always be in the same format, regardless of the number of possible solutions. So for example, instead of outputting the solution if it is unique, or the number of solutions if not, the output could always be the number of solutions, or the output could always be a representation of all of the solutions (regardless of whether there are zero, one, or many).

Another possibility would be to require a single solution, even when more than one exist (so any valid solution will do). I don't know what will make for the more interesting optimisation problem, so I'm just listing options.

Aftermost2167‭ wrote almost 2 years ago

I see what you mean. The point of the challenge is the decode the input and show how many solutions are possible, and output them, but I thought of lifting the need to display solutions if there were more than one. I suppose I could make it so you should always output the solutions you find. I will edit the post and fix that, thank you!

trichoplax‭ wrote almost 2 years ago · edited almost 2 years ago

I don't know whether it applies to this particular problem, but in general there is a subtle potential difference between asking for solutions to be output, and asking for just the total number of them:

For some types of problem it's possible to count how many solutions exist, without actually finding those solutions (which is much faster than finding the solutions).

If you think there's a chance this problem is of that type, you'd need to decide whether you would be happy for someone to win who finds a way of counting the solutions without calculating them, or whether you'd prefer to require the solutions themselves be output so there can be no shortcuts.

Aftermost2167‭ wrote almost 2 years ago

Yeah, I'm aware of that, and the intention is precisely to find all solutions, not just count them. It makes for a more interesting problem, in my opinion.