Connect 4: Spot the Fake!












8












$begingroup$


The bank has been broken into, and all the local mafia thugs have an unusual alibi: they were at home playing Connect 4! In order to assist with the investigation, you are asked to write a program to validate all the Connect 4 boards that have been seized in order to check that the positions are indeed positions from a valid Connect 4 game, and have not been hastily put together as soon as the police knocked on the door.



For example (with R starting), the following is an impossible Connect 4 position.



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | |
| | |Y| | | | |
|R| |Y| | | | |


Your program or function must take in a Connect 4 board and return either




  • A falsy value, indicating that the position is impossible or

  • A string of numbers from 1 to 7, indicating one possible sequence of moves leading to that position (the columns are numbered 1 to 7 from left to right, and so the sequence 112, for example, indicates a red move in column 1, followed by a yellow move in column 1, followed by a red move in column 2). You may choose a column-numbering other than 1234567 if you like, as long as you specify in your solution. If you want to return the list in some other format; for example as an array [2, 4, 3, 1, 1, 3] then that is fine too, as long as it is easy to see what the moves are.


You can choose to read the board in in any sensible format including using letters other than R and Y for the players, but you must specify which player goes first. You can assume that the board will always be 6x7, with two players.



This is code golf, so shortest answer wins. Standard loopholes apply.



Examples



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> 1234567 (one possible answer)
| | | | | | | |
|R|Y|R|Y|R|Y|R|

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | | --> false
| | |Y| | | | |
|R| |Y| | | | |

| | | | | | | |
| | |Y| | | | |
| | |R| | | | |
| | |Y| | | | | --> 323333 (only possible answer)
| | |R| | | | |
| |Y|R| | | | |

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> false (this is the position arising after
| |Y|Y|Y|Y| | | the moves 11223344, but using those moves
| |R|R|R|R| | | the game would have ended once R made a 4)









share|improve this question











$endgroup$












  • $begingroup$
    Could you, eg, take the board as arrays of characters representing the rows, or do you have to parse the ascii with the |, etc?
    $endgroup$
    – Jonah
    4 hours ago










  • $begingroup$
    @Jpnah You can read the board in using any sensible format, including as arrays.
    $endgroup$
    – John Gowers
    4 hours ago


















8












$begingroup$


The bank has been broken into, and all the local mafia thugs have an unusual alibi: they were at home playing Connect 4! In order to assist with the investigation, you are asked to write a program to validate all the Connect 4 boards that have been seized in order to check that the positions are indeed positions from a valid Connect 4 game, and have not been hastily put together as soon as the police knocked on the door.



For example (with R starting), the following is an impossible Connect 4 position.



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | |
| | |Y| | | | |
|R| |Y| | | | |


Your program or function must take in a Connect 4 board and return either




  • A falsy value, indicating that the position is impossible or

  • A string of numbers from 1 to 7, indicating one possible sequence of moves leading to that position (the columns are numbered 1 to 7 from left to right, and so the sequence 112, for example, indicates a red move in column 1, followed by a yellow move in column 1, followed by a red move in column 2). You may choose a column-numbering other than 1234567 if you like, as long as you specify in your solution. If you want to return the list in some other format; for example as an array [2, 4, 3, 1, 1, 3] then that is fine too, as long as it is easy to see what the moves are.


You can choose to read the board in in any sensible format including using letters other than R and Y for the players, but you must specify which player goes first. You can assume that the board will always be 6x7, with two players.



This is code golf, so shortest answer wins. Standard loopholes apply.



Examples



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> 1234567 (one possible answer)
| | | | | | | |
|R|Y|R|Y|R|Y|R|

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | | --> false
| | |Y| | | | |
|R| |Y| | | | |

| | | | | | | |
| | |Y| | | | |
| | |R| | | | |
| | |Y| | | | | --> 323333 (only possible answer)
| | |R| | | | |
| |Y|R| | | | |

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> false (this is the position arising after
| |Y|Y|Y|Y| | | the moves 11223344, but using those moves
| |R|R|R|R| | | the game would have ended once R made a 4)









share|improve this question











$endgroup$












  • $begingroup$
    Could you, eg, take the board as arrays of characters representing the rows, or do you have to parse the ascii with the |, etc?
    $endgroup$
    – Jonah
    4 hours ago










  • $begingroup$
    @Jpnah You can read the board in using any sensible format, including as arrays.
    $endgroup$
    – John Gowers
    4 hours ago
















8












8








8





$begingroup$


The bank has been broken into, and all the local mafia thugs have an unusual alibi: they were at home playing Connect 4! In order to assist with the investigation, you are asked to write a program to validate all the Connect 4 boards that have been seized in order to check that the positions are indeed positions from a valid Connect 4 game, and have not been hastily put together as soon as the police knocked on the door.



For example (with R starting), the following is an impossible Connect 4 position.



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | |
| | |Y| | | | |
|R| |Y| | | | |


Your program or function must take in a Connect 4 board and return either




  • A falsy value, indicating that the position is impossible or

  • A string of numbers from 1 to 7, indicating one possible sequence of moves leading to that position (the columns are numbered 1 to 7 from left to right, and so the sequence 112, for example, indicates a red move in column 1, followed by a yellow move in column 1, followed by a red move in column 2). You may choose a column-numbering other than 1234567 if you like, as long as you specify in your solution. If you want to return the list in some other format; for example as an array [2, 4, 3, 1, 1, 3] then that is fine too, as long as it is easy to see what the moves are.


You can choose to read the board in in any sensible format including using letters other than R and Y for the players, but you must specify which player goes first. You can assume that the board will always be 6x7, with two players.



This is code golf, so shortest answer wins. Standard loopholes apply.



Examples



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> 1234567 (one possible answer)
| | | | | | | |
|R|Y|R|Y|R|Y|R|

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | | --> false
| | |Y| | | | |
|R| |Y| | | | |

| | | | | | | |
| | |Y| | | | |
| | |R| | | | |
| | |Y| | | | | --> 323333 (only possible answer)
| | |R| | | | |
| |Y|R| | | | |

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> false (this is the position arising after
| |Y|Y|Y|Y| | | the moves 11223344, but using those moves
| |R|R|R|R| | | the game would have ended once R made a 4)









share|improve this question











$endgroup$




The bank has been broken into, and all the local mafia thugs have an unusual alibi: they were at home playing Connect 4! In order to assist with the investigation, you are asked to write a program to validate all the Connect 4 boards that have been seized in order to check that the positions are indeed positions from a valid Connect 4 game, and have not been hastily put together as soon as the police knocked on the door.



For example (with R starting), the following is an impossible Connect 4 position.



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | |
| | |Y| | | | |
|R| |Y| | | | |


Your program or function must take in a Connect 4 board and return either




  • A falsy value, indicating that the position is impossible or

  • A string of numbers from 1 to 7, indicating one possible sequence of moves leading to that position (the columns are numbered 1 to 7 from left to right, and so the sequence 112, for example, indicates a red move in column 1, followed by a yellow move in column 1, followed by a red move in column 2). You may choose a column-numbering other than 1234567 if you like, as long as you specify in your solution. If you want to return the list in some other format; for example as an array [2, 4, 3, 1, 1, 3] then that is fine too, as long as it is easy to see what the moves are.


You can choose to read the board in in any sensible format including using letters other than R and Y for the players, but you must specify which player goes first. You can assume that the board will always be 6x7, with two players.



This is code golf, so shortest answer wins. Standard loopholes apply.



Examples



| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> 1234567 (one possible answer)
| | | | | | | |
|R|Y|R|Y|R|Y|R|

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | | --> false
| | |Y| | | | |
|R| |Y| | | | |

| | | | | | | |
| | |Y| | | | |
| | |R| | | | |
| | |Y| | | | | --> 323333 (only possible answer)
| | |R| | | | |
| |Y|R| | | | |

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> false (this is the position arising after
| |Y|Y|Y|Y| | | the moves 11223344, but using those moves
| |R|R|R|R| | | the game would have ended once R made a 4)






code-golf board-game






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago







John Gowers

















asked 6 hours ago









John GowersJohn Gowers

1619




1619












  • $begingroup$
    Could you, eg, take the board as arrays of characters representing the rows, or do you have to parse the ascii with the |, etc?
    $endgroup$
    – Jonah
    4 hours ago










  • $begingroup$
    @Jpnah You can read the board in using any sensible format, including as arrays.
    $endgroup$
    – John Gowers
    4 hours ago




















  • $begingroup$
    Could you, eg, take the board as arrays of characters representing the rows, or do you have to parse the ascii with the |, etc?
    $endgroup$
    – Jonah
    4 hours ago










  • $begingroup$
    @Jpnah You can read the board in using any sensible format, including as arrays.
    $endgroup$
    – John Gowers
    4 hours ago


















$begingroup$
Could you, eg, take the board as arrays of characters representing the rows, or do you have to parse the ascii with the |, etc?
$endgroup$
– Jonah
4 hours ago




$begingroup$
Could you, eg, take the board as arrays of characters representing the rows, or do you have to parse the ascii with the |, etc?
$endgroup$
– Jonah
4 hours ago












$begingroup$
@Jpnah You can read the board in using any sensible format, including as arrays.
$endgroup$
– John Gowers
4 hours ago






$begingroup$
@Jpnah You can read the board in using any sensible format, including as arrays.
$endgroup$
– John Gowers
4 hours ago












1 Answer
1






active

oldest

votes


















2












$begingroup$

JavaScript (ES6),  202  188 bytes



Takes input as a matrix with $1$ for red, $2$ for yellow and $0$ for empty. Returns a string of 0-indexed moves (or an empty string if there's no solution). Reds start the game.





m=>(p=[...'5555555'],g=(c,s=o='')=>/1|2/.test(m)?[1,13,15,17].some(n=>eval(`/(5|6)(.{${n}}\1){3}/`).test(m))?o:p.map((y,x)=>m[(m[y][x]|=4)^c||p[g(c^3,s+x,p[x]--),x]++,y][x]&=3)&&o:o=s)(5)


Try it online!






share|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "200"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178944%2fconnect-4-spot-the-fake%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2












    $begingroup$

    JavaScript (ES6),  202  188 bytes



    Takes input as a matrix with $1$ for red, $2$ for yellow and $0$ for empty. Returns a string of 0-indexed moves (or an empty string if there's no solution). Reds start the game.





    m=>(p=[...'5555555'],g=(c,s=o='')=>/1|2/.test(m)?[1,13,15,17].some(n=>eval(`/(5|6)(.{${n}}\1){3}/`).test(m))?o:p.map((y,x)=>m[(m[y][x]|=4)^c||p[g(c^3,s+x,p[x]--),x]++,y][x]&=3)&&o:o=s)(5)


    Try it online!






    share|improve this answer











    $endgroup$


















      2












      $begingroup$

      JavaScript (ES6),  202  188 bytes



      Takes input as a matrix with $1$ for red, $2$ for yellow and $0$ for empty. Returns a string of 0-indexed moves (or an empty string if there's no solution). Reds start the game.





      m=>(p=[...'5555555'],g=(c,s=o='')=>/1|2/.test(m)?[1,13,15,17].some(n=>eval(`/(5|6)(.{${n}}\1){3}/`).test(m))?o:p.map((y,x)=>m[(m[y][x]|=4)^c||p[g(c^3,s+x,p[x]--),x]++,y][x]&=3)&&o:o=s)(5)


      Try it online!






      share|improve this answer











      $endgroup$
















        2












        2








        2





        $begingroup$

        JavaScript (ES6),  202  188 bytes



        Takes input as a matrix with $1$ for red, $2$ for yellow and $0$ for empty. Returns a string of 0-indexed moves (or an empty string if there's no solution). Reds start the game.





        m=>(p=[...'5555555'],g=(c,s=o='')=>/1|2/.test(m)?[1,13,15,17].some(n=>eval(`/(5|6)(.{${n}}\1){3}/`).test(m))?o:p.map((y,x)=>m[(m[y][x]|=4)^c||p[g(c^3,s+x,p[x]--),x]++,y][x]&=3)&&o:o=s)(5)


        Try it online!






        share|improve this answer











        $endgroup$



        JavaScript (ES6),  202  188 bytes



        Takes input as a matrix with $1$ for red, $2$ for yellow and $0$ for empty. Returns a string of 0-indexed moves (or an empty string if there's no solution). Reds start the game.





        m=>(p=[...'5555555'],g=(c,s=o='')=>/1|2/.test(m)?[1,13,15,17].some(n=>eval(`/(5|6)(.{${n}}\1){3}/`).test(m))?o:p.map((y,x)=>m[(m[y][x]|=4)^c||p[g(c^3,s+x,p[x]--),x]++,y][x]&=3)&&o:o=s)(5)


        Try it online!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 9 mins ago

























        answered 1 hour ago









        ArnauldArnauld

        73.6k689309




        73.6k689309






























            draft saved

            draft discarded




















































            If this is an answer to a challenge…




            • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


            • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
              Explanations of your answer make it more interesting to read and are very much encouraged.


            • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



            More generally…




            • …Please make sure to answer the question and provide sufficient detail.


            • …Avoid asking for help, clarification or responding to other answers (use comments instead).





            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178944%2fconnect-4-spot-the-fake%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to label and detect the document text images

            Tabula Rosettana

            Aureus (color)