Can this village have a wedding?

26

2

The quaint hamlet of Кодгольф in the Russian far east has a problem: their population is low (below 66), and no new people have arrived for years. Moreover, after centuries of near-isolation, just about everybody is related to each other.

Mayor Стекобмен has a solution that should keep the morale high: organise a wedding. However, the question is, are there two bachelors in the town that aren't at least cousins of each other?

The mayor fired up his state-of-the-art Microsoft Боб computer to consult the genealogy charts. They had just been updated to the brand-new ASCII format, and look like this:

ы┬К
 ю

And this:

 А┬д
 О┴п┬Щ 
    Ф   

And this:

  з┬Й
Л┬м┴п─┬Ф
Ы┴я┬А ш┬Я
   З   Е

And even this:

 ю┬────────Й
м┬Е    
 │     ш
 │
 │
 Щ

Here's how it works. Each person is a letter from the Russian alphabet. Males are capital letters (any of АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ), females are lowercase letters (any of абвгдеёжзийклмнопрстуфхцчшщъыьэюя).

A '┬' between two people means they are married. Right below that is either another person - their child - or a '┴', meaning that this couple has two children; located to the left and right of the symbol.

Moreover, to the right and left of '┬' and '┴' there can be any number of '─' characters, to extend the lines, as it were. Similarly, there can be any number of '│' characters above a '┴' or below a '┬'.

Lastly, a character without any defined symbol above them is considered a new arrival to the village, and by definition unrelated to anybody.

Also be aware that this is a very conservative village. Nobody marries more than once, and every marriage is heterosexual. Furthermore, assume everybody in the graph is alive, and no two people share the same name: e.g., the same Cyrillic character never occurs more than once.

The two bachelors should be of the opposite gender, and they should not be first cousins or any more closely related. First cousins once removed is okay. In other words: they should not share a parent, or a grandparent, or have one's grandparent be another's parent.

Challenge

Make the shortest possible program with as input either a string (containing newline characters), or a string array, or a rectangular two-dimensional string or char array (no higher or wider than 100 characters), containing the family tree of the town. Assume the input is a valid family tree. Ignore any character not given an explicit function in the above description.

It's fine to use an encoding other than Unicode for input/output purposes, e.g. KOI8-R as long as it's known and standardised by someone other than yourself. Scoring is done per this answer. If your language handles Cyrillic natively, it's an advantage!

Return a boolean value of true or false (or a bit/int of 1 or 0, or any consistent truthy/falsey value used by the language of your choice) to indicate whether there can be a wedding given the family tree.

Examples

1.

ы┬К   
 ю    

FALSE (there's only one bachelor)

2.

   А┬д    
 ю┬О┴п┬Щ  
  Щ   ф   

FALSE (Щ and ф are cousins)

3.

  з┬Й     
Л┬м┴п─┬Ф  
Ы┴я┬А ш┬Я 
   З   Е  

FALSE (З, Е and Ы are all male)

4.

  з┬Й     
Л┬м┴п─┬Ф  
ё┴А┬я ш┬Я 
   З   Е  

TRUE (ё and Е can marry)

5.

             ю┬────────Й  
            м┬Е           
             │     ш      
             │            
             │            
             Щ            

TRUE (ш is not related to anybody)

6.

           д┬Ф                                
 ю┬────────Й┴─ё┬З                             
  │            │                              
  │            ш <this person smells bad      
  │                                           
  │                                           
  Щ <this person betrayed the Party!          

FALSE (Щ and ш are cousins)

7.

Щ 1234567890 quick brown foxes jumped over the lazy dog ш

TRUE (Щ and ш are unrelated)

8.

    з┬Й     
  Л┬м┴п─┬Ф  
й┬Ы┴я┬А ш┬Я 
 Э   З   ю  

TRUE (ю and З, and also ю and Э)

9.

 А┬д      ф┬Ж   
 м┴п┬Щ     ю┬Я  
    Ф       ц   

TRUE (ц and Ф)

NOTE: Here is an ASCII version of roughly the same challenge.

KeizerHarm

Posted 2019-11-15T16:52:36.207

Reputation: 687

2Hi KeizerHarm, welcome to the site! Thanks for the question, it looks fun. A question: For example 4, aren't ё and З barred from marriage due to the fact that ё is З's aunt? In other words, ё's parent Л is З's grandparent? Also, I take it that we are to assume that marriages are always heterosexual, and that divorce and remarrying of widows/widowers does not exist? Making those assumptions is fine by me, I just wanted to be sure. – isaacg – 2019-11-15T17:38:40.563

1@isaacg Thanks. You're right about question 4; I missed that. I'll fix that right away, and then draw up another example with two possible marriages.

And yes to all of those. I'll add that too. – KeizerHarm – 2019-11-15T17:39:55.417

1Also for 6, shouldn't Щ and ш be allowed to marry because they are first cousin's once removed, not first cousins? Щ's great grandparents are д and Ф, who are ш's grandparents. – isaacg – 2019-11-15T17:40:47.433

1...I should really triple-check these things ^^; Thank you, I'll fix it right away. – KeizerHarm – 2019-11-15T17:41:59.500

Can we assume that no characters in the Unicode range from U+0400 to U+04FF will appear in the input other than those you've specified? Or do we need to handle characters like ө for example? – Nick Kennedy – 2019-11-15T19:07:31.927

1@NickKennedy Only expect Cyrillic, the box-drawing characters specified, and printable ASCII characters. – KeizerHarm – 2019-11-15T19:13:54.407

1@KeizerHarm what about Cyrillic characters not in your list of men and women? – Nick Kennedy – 2019-11-15T19:14:31.757

1@NickKennedy No need to take those into account. – KeizerHarm – 2019-11-15T19:16:49.120

10I've read through this a couple of times but maybe I'm still missing something: I don't see what requiring the use of the Russian alphabet adds to this challenge. – Shaggy – 2019-11-15T23:41:06.333

3@Shaggy Well, for one: were it in the Latin alphabet, to detect a person you could simply check a character against the range [a-zA-Z]. The Russian alphabet however is not on one continuous range, so either Ё&ё need to be treated separately or the entire string of allowed characters needs to be in there, and I wonder if you could even compress that. Plus this makes for a funnier story. – KeizerHarm – 2019-11-16T00:19:43.747

8Welcome to CGCC! I like your idea, but I'll agree with Shaggy: the really interesting core of this challenge has to do with first decoding the input into a graph of familial relationships, and then making some deductions based on that graph. The Cyrillic requirement is just an annoying distraction from those tasks, especially in languages where unicode is a bit of a pain. Maybe as a separate challenge "Is this Cyrillic unicode letter a capital letter?" would be interesting to golf for some; but as it is, it really detracts from my interest in answering your question. Just sayin'! – Chas Brown – 2019-11-16T04:19:35.707

@ChasBrown Thanks for the feedback! I understand the issue; it's not something I really considered when I thought this up. I will definitely keep that sorta thing in mind if I come up with another puzzle. I'm not sure it would be proper form to just rewrite this question when there's been a whole discussion about character encoding (in now-deleted comments) and there's a chance someone already got started on a program with Cyrillic specifically in mind. – KeizerHarm – 2019-11-16T10:04:01.193

I always thought a female bachelor was called a spinster. But I guess these days women want to use male terms for everything... – Neil – 2019-11-16T13:16:24.540

@Neil Not sure what the gender-neutral term was... sorry, not an Anglophone ^^; – KeizerHarm – 2019-11-16T17:22:55.357

@Others I think if this question gets no answers after a couple of days, I'll leave it be but re-ask it without Cyrillics. I just don't want to piss off anyone of the 16 upvotes who might be working on it already. – KeizerHarm – 2019-11-16T17:22:57.560

Might the same letter occur multiple times if necessary to draw the graph? – Peter Taylor – 2019-11-18T08:54:05.563

@PeterTaylor It's your job to interpret the graph, not to draw it. The test cases do not ever repeat a letter. – KeizerHarm – 2019-11-18T09:01:34.813

The test cases might not, but I don't see any statement either way as to whether valid input might. – Peter Taylor – 2019-11-18T09:03:01.947

@PeterTaylor Alright; I'll clarify that. – KeizerHarm – 2019-11-18T09:09:18.823

I made a non-Cyrillic version: https://codegolf.stackexchange.com/questions/196035/can-this-scottish-village-have-a-wedding

– KeizerHarm – 2019-11-18T13:20:50.107

Answers

2

Jelly, 160 159 bytes

Ø.UN,ƊAN,Ɗ+Ṫ¥+œị2,5yⱮ$ɼ=5,6⁼Ø.Ɗɗ¡ƬṪ¥ƒ⁸’1¦⁺œị®⁼5ƊпṖṪ+2¦œị®⁻.Ɗ¡ƬṪ¥ⱮØ+$“”¹?
Odȷ%⁴ỊḢịƊ€€H“¥©“©©‘;U¤œṣjƭƒ$€ƬṪ©=.ŒṪ+2¦œị®ɗⱮØ+f2,4ƊÐḟWÇ€Ẏ$Ƭḣ3ẎƲ€Œcf/ÐḟḢ€€ȧœị¥O>⁽¡FIFẸ

Try it online!

A monadic link that takes a list of Jelly strings and returns 1 for true and 0 for false.

I’m sure this could be golfed more. Full explanation to follow.

Nick Kennedy

Posted 2019-11-15T16:52:36.207

Reputation: 11 829

6

K (ngn/k), 230 203 200 196 bytes

{t:{x*\:x};n:#*x:4(+|0,)/x;p:&2!c:+/"╒ё╡Ё©ъъ"<\:a:,/x;|//(~h=\:h:4!c p)&t[~|/p=/:?,/2#'(p^p^)'g]&3<{&/'x+\:x}/(~=#p)*(#a;1)0|/t'?(+/'p=/:/:g:{?'x,/'x x}/(!#a)+(-e;e,:n;e;n*e:-1 1;())4^"┴┬─│"?a)^0}

the input and the k code must be encoded in koi8-r. test with (linux only):

git clone https://bitbucket.org/ngn/k
cd k/g
../k can-this-village-have-a-wedding.k

please make sure your editor handles koi8-r correctly. for instance, if using vim, you can type :e ++enc=koi8-r after opening the file or put set fencs=utf-8,koi8-r in your ~/.vimrc


k functions are written in { }, have an implicit argument x, and consist of ;-separated expressions.

the sequence of expressions is evaluated left-to-right but the code within each expression is right-to-left.

t:{x*\:x} helper function that creates a multiplication table (outer product) for a list x

x:4(+|0,)/x surrounds the input x with zeroes. literally: 4 times (4( )/) add zero(-es) on top (0,), reverse (|), and transpose (+).

n:#*x let n be the width of the input. literally: length (#) of the first (*)

a:,/x let a be the flattened input

c:+/"╒ё╡Ё©ъъ"<\:a for each character in a, count how many of "╒ё╡Ё©ъъ"are before it (in koi8-r). this will give odd numbers for russian letters and even for non-letters. also, the remainder mod 4 will indicate the sex - upper/lowercase.

p:&2!c take c mod 2 (2!) and make a list of the indices where (&) it's 1. "p" for "people".

the rest of the code will build three p×p matrices that represent conditions for marriage:

  • the couple must be >3 steps apart in the graph of relatives. a "step" is a relationship of parent-child or spouse or sibling.

    • 4^"┴┬─│"?a for each in a find its index among "┴┬─│" and fill in 4 if not found.

    • (-e;e,:n;e;n*e:-1 1;()) replace "┴" with (-1;1;-n), "┬" with (-1;1;n), "─" with (-1;1), "│" with (-n;n), and others with an empty list

    • (!#a)+ add 0 1 2.. thus creating lists of neighbours

    • g:{?'x,/'x x}/ transitive closure - extend (,/) each (') neighbour list (x) with the neighbour lists of its neighbours (x x) and unique it (?), until convergence ({ }/); assign to g for "graph"

    • +/'p=/:/:g for each of g's neighbour lists build a boolean mask for which people are in it. ignore non-people.

    • ?( )^0 remove scalar 0s (( )^0) as they are a by-product of empty neighbour lists, and make the rest unique (?). this gives us a list of families as boolean masks.

    • t' build a family matrix for each family

    • 0|/ boolean-or of all family matrices

    • (#a;1) replace 0s with "infinity" (the length of a is as good as infinity here) and keep 1s as they are - this is a graph of how closely related p are. we need to find the shortest paths in it.

    • (~=#p)* put 0s on the diagonal. literally: multiply (*) by the negation (~) of the unit matrix (=) of that size (#p)

    • {&/'x+\:+x}/ until convergence, try to improve dist(i,j) with dist(i,k)+dist(k,j) (similar to the floyd-warshall algorithm)

    • 3< more distant than first cousins

  • they must not be already married

    • t[~|/p=/:?,/2#'(p^p^)'g] check which p are among the first 2 (2#) of the intersection between people ((p^p^)) and each (') neighbour list in g, and make it a boolean table
  • they must be of opposite sexes

    • (~h=\:h:4!c p) remember that c mod 4 encodes gender information

finally, |//...&...&... and-s the three matrices and tests if there's a truthy element in the result

ngn

Posted 2019-11-15T16:52:36.207

Reputation: 11 449

2Yes, we have our first entry! Would you mind unwrapping it for me? I'd love to see your approach. – KeizerHarm – 2019-11-18T08:00:30.997

Ah, thank you. Count me impressed! We'll see if someone else dares to complete this challenge... – KeizerHarm – 2019-11-18T13:52:09.020