26
2
Background
The United States has a unique love of gerrymandering––the deliberate manipulation of an electoral district to predict certain voting results. Just recently there was a gerrymandering case brought before the Supreme Court. Gerrymandering, especially when related to race, is ruled illegal and results in the requirement to redraw the district lines.
Given a rectangular map of a municipality (2d array), you will draw district lines to help your party get the most representation. That is, you will gerrymander. Every municipality has two parties, 0
and 1
. The map will be comprised of squares with either 0
or 1
on them. Here is an example map:
Challenge
You will group the map into districts so that the 1
party will get at least the number of districts specified by the Input.
Input
The input will consist of a map, the number of districts to draw, and the minimum number of districts the 1
party needs to win (the minimum score).
Output
The output will be a map of the districts. Each district will be uniquely comprised of a capitalized letter of the alphabet. Yes, this means that there will not be more than 26 districts.
If there is no possible output where the inputted party wins enough districts, either:
- Print “We tried...”
- Fatally error because the party was irreparably injured by the election results
- Or both
Rules (also very important)
- All districts must be contiguous
- Districts may not have other districts in them
- Each district must have at least four nodes in it. The input will be consistent with the rules, meaning that there will be at least
number_of_districts * 4
nodes in the map - The score of each party is the number of districts it has a majority in
- If a district has the same number of
0
s and1
s, then neither party benefits from it - Normal no-cheating rules
- This is code-golf, so shortest code in bytes wins.
Test cases
1. Input 1. Output 2. Input 2. Output 3. Input 3. Output
districts: 5 Image and map districts: 3 Image below districts: 3 fatal error
min wins: 3 min wins: 3 min wins: 3
map: map: map:
00000110000 AAAAAAAAAAA 101101 101101
10000010000 AAAAAAAAAAA 100000 100000
10010000011 AAAAAAAAAAA 011011 011011
11001110000 BBBBBBBAAAA 111111 100111
00111111000 BBBBBBBAAAA
01111111000 CCCCCDDDAAA
01111111001 CCCCCDDDAAA
01000111100 EEEEEDDDDDD
00000001000 EEEEEDDDDDD
Of course, your program should work for any valid test case, not just these ones.
@Arnauld, yes they are only illustrative. The real output should be like in the first test case with the letters of the alphabet. I changed the tag to reflect this. – Daniel – 2017-11-29T22:39:15.273
A simple partition of the first test case would be something like this. Is that correct?
– Arnauld – 2017-11-29T22:53:34.007@Arnauld, yes, that's valid. – Daniel – 2017-11-29T23:00:34.923
So for the 3rd example, if we divided it into horizontal rows, each 1 district tall, then the 1s would win 3 to 1 yes? – Michael Dorgan – 2017-11-29T23:20:02.373
@MichaelDorgan, yes, except that you may only have three districts in that one. – Daniel – 2017-11-29T23:21:41.910
Ah - hence the fail. Missed the district count. Thanks – Michael Dorgan – 2017-11-29T23:35:53.850
3This reminds me a whole lot of what had to be done for char based graphics on Nintendo handhelds from DMG up to DS. You were given specific shapes to cut graphics into and had to minimize the number of shapes used as you could only have a hardware defined number of sprites (shapes) in use. That was not an easy problem. – Michael Dorgan – 2017-11-29T23:39:10.060
I think this problem may be more suitable for fastest-code? – Colera Su – 2017-11-30T02:04:37.133
interesting, tough problem. is there an efficient algorithm for this? what is its complexity? – Jonah – 2017-12-01T00:54:49.817
"districts must not have other districts within them" means that something like
AAA ABA AAA
wouldn't be allowed, right? – Giuseppe – 2017-12-01T15:13:22.520@Giuseppe, exactly – Daniel – 2017-12-01T15:25:04.433
What is the format of the 'map'? String with newlines? Sequence of strings? 2D Array of booleans? – Οurous – 2017-12-05T20:06:34.583
@Ourous, any of those works. – Daniel – 2017-12-05T20:26:27.817