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 »
Challenges

Comments on Decoding a non injective bit matrix encoding

Post

Decoding a non injective bit matrix encoding

+4
−0

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 1s in each line (top-to-bottom)
  • S integers corresponding to the number of 1s in each column (left-to-right)
  • 2 integers corresponding to the number of 1s in each diagonal (main, then anti-diagonal)
  • 4 integers corresponding to the number of 1s 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 4
0 2 2 0
1 2 2 1
1 2 2 1

Decoded matrix

1
0001
0010
0100
1000

Scoring

The goal for this challenge is the produce the fastest algorithm (i.e, the algorithm with the smallest asymptotic complexity), and as such you should include an short analysis of your algorithm alongside your code.

Despite that, all solutions will be evaluated by me on the same machine, and the time measurements posted as a comment on the corresponding answer. This is merely for curiosity, not for actual scoring.
You can expect the latest versions for each language and compiler/interpreter. For languages with JITs and similar tools, those will be used (e.g for Python, PyPy will be used).

If you manage to get your decoder to run in under a second for the bigger cases, you will beat me :D

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.
Why should this post be closed?

2 comment threads

Asymptotic complexity versus measured time (5 comments)
Welcome! (14 comments)
Asymptotic complexity versus measured time
trichoplax‭ wrote almost 2 years ago

I notice the scoring method has been changed from measured time to asymptotic complexity. This is a perfectly valid choice - I just wanted to mention my thoughts on the difference in case it affects your decision:

trichoplax‭ wrote almost 2 years ago

Measured time is concrete - there is no doubt who is at the top of the leaderboard. Asymptotic complexity is only objective in a mathematical setting. In the real world people can often disagree on what counts as a given complexity. Even for relatively simple programs the use of built in methods may mean the underlying order of complexity may be higher than it appears from the source code. Measured time removes this opportunity for disagreement.

trichoplax‭ wrote almost 2 years ago

Measured time introduces the possibility of friendly rivalry between different programming languages. Coding challenges often have more than one aspect of competition: both competing between different answers in the same language, and competing between different languages. With asymptotic complexity, once the best complexity is reached in one language, there's nothing any other language can do to beat that.

trichoplax‭ wrote almost 2 years ago

Asymptotic complexity is intentionally simplified. That's what makes it so useful for analysis. However, this also takes away the fine tuning aspect of optimisation competition. Measured time leads to open ended competition where the contestant can continually find little ways to shave off a few milliseconds, moving up and down the leaderboard as new insights are found. With asymptotic complexity $N^2$ is $N^2$. There's no reward for optimising it down to $0.999N^2$ - that's still just $N^2$ and gives no advantage on the leaderboard. Once one person has found an $O(N^2)$ solution, people can implement that same algorithm in every other language, and now they are all joint 1st place on the leaderboard. With measured time the algorithm that performs best in C might not be the best approach to take in Python. The messiness of the real world makes for a more interesting challenge.

trichoplax‭ wrote almost 2 years ago

I'm very interested to see how this competition goes either way, but my personal opinion is that it would be more interesting as a fastest-code challenge than an asymptotic-complexity challenge.