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 moderator attention?
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)
Welcome!
trichoplax‭ wrote about 1 year ago

Welcome to Code Golf Codidact! This looks like an interesting challenge.

It might need some fine tuning to make it clear how to calculate the score for generating the automated leaderboard. For example, the run time will differ on different machines. We usually post our challenge ideas in the Sandbox first to get feedback, and then post here once all the details are finalised. Feel free to post there if you'd like to go through that process (it's easier to make changes in the Sandbox, where there are no answers that would be invalidated by changes).

In general, fastest code challenges are more difficult to run than code golf challenges. For a code golf challenge, the number of bytes in the source code is the same on anyone's machine, so each competitor can score their own code. For a fastest code challenge, extra work is needed to ensure each competitor's score is fair (not advantaged by them having a faster machine).

Aftermost2167‭ wrote about 1 year ago

Thank you! I missed that sandbox, my bad :/ I will re-post there. Should we delete this or keep it and then update it once the details are set in the sandbox? Either way, the idea here was really to make it as fast as possible, so I would be down to benchmarking every answer on my machine, if that's alright.

trichoplax‭ wrote about 1 year ago

We don't yet have any examples of fastest-code challenges - yours would be the first.

If you made this a code golf challenge (shortest code) then it would be simpler for you, but would still benefit from some test cases.

If you keep it as a fastest code challenge then ideally you would both provide test cases and specify how the timings will be measured. You could say you will run the code from each answer on your own machine so that it's fair, but that would require you to install all the programming languages used by all the answers, and to trust all of the code to run on your own machine. Many languages have places where code can be run online, so it might be possible to use those rather than running on your own machine. I'd be interested to hear if anyone has any ideas on how to make running a fastest code challenge easier.

trichoplax‭ wrote about 1 year ago

I missed your post while typing - sounds like you're fine with running on your own machine. Once you have copied this into the sandbox you can simply press delete on this one - it will remain visible to you but no one will be able to post an answer which means you'll be free to change whatever you choose once you're happy with the sandbox version.

trichoplax‭ wrote about 1 year ago

(You can always "undelete" your own posts that you have deleted, so it works as a way of temporarily preventing answers.)

Aftermost2167‭ wrote about 1 year ago · edited about 1 year ago

https://codegolf.codidact.com/comments/thread/7238#comment-19344 Ahah exciting! I will delete/hide this post while we suss out the details on the sandbox then :) Here it is: https://codegolf.codidact.com/posts/287928

trichoplax‭ wrote about 1 year ago

This is looking good.

For the Time constraints section, it would be helpful to specify what size input we need to beat 20 seconds for. It would be good to have at least one test case of this size so we can make sure our solutions are acceptable before posting them.

Aftermost2167‭ wrote about 1 year ago

Indeed, I will add those later today.

trichoplax‭ wrote about 1 year ago

Might also be handy to have at least one test case with more than one matrix as output. In addition to being a useful test of the code, it will also show the output format you expect for multiple matrices.

trichoplax‭ wrote about 1 year ago · edited about 1 year ago

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)

The example and the test cases appear to be showing the number of 1s in each line and column, rather than the number of 0s.

Should the rules or the test cases be different?

trichoplax‭ wrote about 1 year ago

The main diagonal in the example is listed as having no zeros, but the output shows 4 zeros.

The antidiagonal in the example is listed as having 2 zeros, but the output shows no zeros (all ones).

The main diagonal and antidiagonal in the second test case are both listed as having no zeros, but the output shows 3 zeros for both.

Aftermost2167‭ wrote about 1 year ago

You're completely right! I mistyped the statement, it should indeed be 1s and not 0s, my bad. Same for the example case, the anti-diagonal should be 4.

Aftermost2167‭ wrote about 1 year ago

I've been thinking and I think it's just simpler and better to remove the time constraint altogether. It doesn't really add any real competitive value, since the point is already to get it as fast as possible, but doesn't discourage answers with esoteric languages or approaches, which may be interesting to learn about.

Monica Cellio‭ wrote about 1 year ago

Aftermost2167‭ welcome to Codidact and thank you for bringing this challenge here! trichoplax‭, thanks for all the helpful guidance!