Sự phát triển của 'x'


15

Cho là một bảng có kích thước thay đổi với kích thước tối đa 5 lần 5 trường. Mỗi trường kann được điền với một 'x'. Nếu nó không được lấp đầy bằng 'x', thì nó chứa đầy 'o'.

Trạng thái bắt đầu của mỗi bảng được đưa ra (xem bên dưới). Với mỗi bảng, phải chơi 10 vòng (tối đa, điều kiện: xem bên dưới) và sự tiến hóa của x phải được theo dõi.

Một vòng hoạt động theo cách sau:

  1. mọi 'x' lan sang các trường có biên giới trực giao, nhưng tự biến mất
  2. mỗi lần hai 'x' nằm trên một trường, chúng trung hòa lẫn nhau

Sự tiến hóa của tất cả 'x' trong mỗi vòng phải diễn ra đồng thời. Thí dụ:

    o o o            o x o
    o x o     ->     x o x
    o o o            o x o

Với mỗi vòng tiến hóa, bạn phải xem liệu bảng có bị trống 'x' không. Nó không trống rỗng, một mô hình lặp đi lặp lại có thể có mặt. Nếu đây cũng không phải là trường hợp, chúng tôi từ bỏ phân tích về sự tiến hóa. Ngoài ra, bạn phải in ra phần trăm tối đa của các trường x cho mỗi bảng bắt đầu (làm tròn xuống thành toàn bộ số).

Đầu vào:

Dữ liệu đầu vào có thể được tìm thấy ở đây (Pastebin) Dữ liệu này chứa 100 trạng thái bắt đầu. Như đã đề cập, các bảng khác nhau về kích thước. Số lượng hàng được ghi với số n từ 1 đến 5, theo sau là n hàng chỉ chứa 'x' và 'o', đại diện cho mẫu bắt đầu. Mỗi hàng của một bảng có 1 đến 5 trường.

Đầu ra:

Kết quả hoàn chỉnh phải được in ra, một hàng được in cho mỗi bảng bắt đầu theo mẫu sau:

    Round {0-10}: {repetition/empty/giveup}, {0-100} percent maximum-fill

Ví dụ:

Ví dụ 1:

    Input: 2       Starting state: x o x
           xox                     x x
           xx

                          Round 1: x x o
                                   o x

                          Round 2: x o x
                                   o x

                          Round 3: o x o
                                   o o

                          Round 4: x o x   -> The pattern repeats:
                                   o x        It is the same as in round 2,
                                              therefore we stop. Maximum fill was
                                              in the starting state with four times 'x'
                                              of 5 fields altogether,
                                              so we have 4/5 = 80 %.

    Output: Round 4: repetition, 80 percent maximum-fill

Ví dụ 2:

    Input: 1       Starting state: x x
           xx                      

                          Round 1: x x    ->  We already have a repetition, because
                                              the pattern is the same as in the starting
                                              state. The board is always filled 100 %.

    Output: Round 1: repetition, 100 percent maximum-fill

Sau tám ngày, tôi sẽ đánh dấu câu trả lời làm việc với ít nhân vật nhất là người chiến thắng. Ngoài ra, tôi sẽ đăng đầu ra chính xác cho 100 bảng bắt đầu (đầu vào).

Bạn có thể sử dụng ngôn ngữ (lập trình / kịch bản / bất cứ thứ gì) ưa thích của bạn.

Chúc vui vẻ!

PS: Nếu bạn có câu hỏi, hãy hỏi.

PPS: Đối với những người sáng tạo ban đầu: Đối với những người có khả năng nói tiếng Đức, câu hỏi được đặt ra từ ĐỪNG NHẤP VÀO NẾU BẠN KHÔNG MUỐN SPOILERS ở đây . Vì thời gian chính thức để hoàn thành thử thách đã kết thúc, tôi muốn xem liệu ai đó có thể đưa ra một giải pháp ngắn gọn và thanh lịch hay không.

22.04.2014:

Thử thách đã xong! Người chiến thắng được đánh dấu là chấp nhận. Đầu ra đúng:

    Round 10: giveup, 50 percent maximum-fill
    Round 5: empty, 66 percent maximum-fill
    Round 1: repetition, 100 percent maximum-fill
    Round 1: empty, 100 percent maximum-fill
    Round 4: repetition, 100 percent maximum-fill
    Round 4: repetition, 70 percent maximum-fill
    Round 2: repetition, 60 percent maximum-fill
    Round 4: empty, 88 percent maximum-fill
    Round 10: giveup, 50 percent maximum-fill
    Round 5: repetition, 80 percent maximum-fill
    Round 10: repetition, 80 percent maximum-fill
    Round 1: empty, 80 percent maximum-fill
    Round 3: repetition, 60 percent maximum-fill
    Round 4: repetition, 48 percent maximum-fill
    Round 9: empty, 41 percent maximum-fill
    Round 10: giveup, 92 percent maximum-fill
    Round 10: giveup, 53 percent maximum-fill
    Round 10: giveup, 66 percent maximum-fill
    Round 6: repetition, 50 percent maximum-fill
    Round 10: giveup, 88 percent maximum-fill
    Round 10: giveup, 76 percent maximum-fill
    Round 10: giveup, 68 percent maximum-fill
    Round 10: giveup, 40 percent maximum-fill
    Round 10: giveup, 100 percent maximum-fill
    Round 10: giveup, 71 percent maximum-fill
    Round 2: empty, 81 percent maximum-fill
    Round 6: repetition, 36 percent maximum-fill
    Round 10: giveup, 61 percent maximum-fill
    Round 10: giveup, 60 percent maximum-fill
    Round 4: repetition, 66 percent maximum-fill
    Round 10: giveup, 72 percent maximum-fill
    Round 3: empty, 80 percent maximum-fill
    Round 10: giveup, 50 percent maximum-fill
    Round 10: giveup, 83 percent maximum-fill
    Round 7: repetition, 37 percent maximum-fill
    Round 9: repetition, 85 percent maximum-fill
    Round 5: repetition, 40 percent maximum-fill
    Round 5: repetition, 60 percent maximum-fill
    Round 4: empty, 80 percent maximum-fill
    Round 10: giveup, 60 percent maximum-fill
    Round 4: repetition, 46 percent maximum-fill
    Round 6: repetition, 42 percent maximum-fill
    Round 10: giveup, 72 percent maximum-fill
    Round 4: repetition, 70 percent maximum-fill
    Round 4: repetition, 80 percent maximum-fill
    Round 6: repetition, 50 percent maximum-fill
    Round 4: repetition, 56 percent maximum-fill
    Round 10: giveup, 60 percent maximum-fill
    Round 10: giveup, 54 percent maximum-fill
    Round 10: giveup, 66 percent maximum-fill
    Round 2: repetition, 40 percent maximum-fill
    Round 2: repetition, 40 percent maximum-fill
    Round 6: repetition, 75 percent maximum-fill
    Round 7: empty, 85 percent maximum-fill
    Round 10: giveup, 50 percent maximum-fill
    Round 6: repetition, 70 percent maximum-fill
    Round 2: empty, 66 percent maximum-fill
    Round 1: empty, 66 percent maximum-fill
    Round 3: empty, 100 percent maximum-fill
    Round 3: empty, 66 percent maximum-fill
    Round 8: repetition, 42 percent maximum-fill
    Round 1: empty, 60 percent maximum-fill
    Round 2: repetition, 100 percent maximum-fill
    Round 2: repetition, 83 percent maximum-fill
    Round 4: repetition, 66 percent maximum-fill
    Round 6: repetition, 75 percent maximum-fill
    Round 4: empty, 66 percent maximum-fill
    Round 10: giveup, 61 percent maximum-fill
    Round 10: giveup, 56 percent maximum-fill
    Round 4: empty, 66 percent maximum-fill
    Round 6: repetition, 33 percent maximum-fill
    Round 3: empty, 57 percent maximum-fill
    Round 3: repetition, 100 percent maximum-fill
    Round 6: repetition, 73 percent maximum-fill
    Round 10: giveup, 50 percent maximum-fill
    Round 6: repetition, 50 percent maximum-fill
    Round 10: giveup, 73 percent maximum-fill
    Round 5: empty, 80 percent maximum-fill
    Round 10: giveup, 61 percent maximum-fill
    Round 3: repetition, 53 percent maximum-fill
    Round 10: giveup, 33 percent maximum-fill
    Round 10: giveup, 80 percent maximum-fill
    Round 10: giveup, 63 percent maximum-fill
    Round 10: giveup, 70 percent maximum-fill
    Round 10: giveup, 84 percent maximum-fill
    Round 7: repetition, 70 percent maximum-fill
    Round 10: repetition, 57 percent maximum-fill
    Round 10: giveup, 55 percent maximum-fill
    Round 6: repetition, 36 percent maximum-fill
    Round 4: repetition, 75 percent maximum-fill
    Round 10: giveup, 72 percent maximum-fill
    Round 10: giveup, 64 percent maximum-fill
    Round 10: giveup, 84 percent maximum-fill
    Round 10: giveup, 58 percent maximum-fill
    Round 10: giveup, 60 percent maximum-fill
    Round 10: giveup, 53 percent maximum-fill
    Round 4: repetition, 40 percent maximum-fill
    Round 4: empty, 40 percent maximum-fill
    Round 10: giveup, 50 percent maximum-fill
    Round 10: giveup, 68 percent maximum-fill

Vui lòng gắn thẻ là mã golf hoặc thử thách mã nhưng không phải cả hai. (Nó nên là mã golf trong trường hợp này).
user80551

1
Ai đó nên thay đổi điều này thành một máy tự động di động được xác định rõ. :-)
Justin

Câu trả lời:


4

Perl, 308, 304, 305, 293, 264 , 262

Chỉnh sửa: Một lỗi xuất hiện sau một trong những chỉnh sửa gần đây, gây ra đầu ra sai cho các bảng trống (đầu ra của bộ kiểm tra là OK). Vì Round 0ở định dạng đầu ra nhất định có thể chỉ có nghĩa là có thể có các bảng trống trong đầu vào (mặc dù không có trong bộ kiểm tra), lỗi phải được sửa. Sửa nhanh có nghĩa là tăng số byte (thực tế là 1) - tất nhiên không phải là một tùy chọn. Do đó, tôi phải đánh gôn thêm một chút nữa.

Chạy với -p(+1 được thêm vào để đếm), đọc từ STDIN. Yêu cầu 5.014 vì rsửa đổi thay thế.

(@a,%h,$m)=('',map<>=~y/ox\n/\0!/rd,1..$_);for$n(0..10){$_="Round $n: ".($h{$_="@a"}++?repetition:(($.=100*y/!///y/ //c)<$m?$.:$m=$.)?giveup:empty).", $m percent maximum-fill\n";@a=/g/?map{$_=$a[$i=$_];y//!/cr&(s/.//r.P^P.s/.$//r^$a[$i+1]^$a[$i-1])}0..$#a:last}

I E

# '-p' switch wraps code into the 'while(<>){....}continue{print}' loop, 
# which reads a line from STDIN into $_, executes '....' and prints contents 
# of $_. We use it to read board height and print current board's result.

# First line reads board's state into @a array, a line per element, at the same 
# time replacing 'o' with 'x00', 'x' with '!' and chomping trailing newlines. 
# '!' was chosen because it's just like 'x01' except 5th bit (which is not important)
# but saves several characters in source code.

# Note: array is prepended with an empty line, which automatically remains in this 
# state during evolution, but saves us trouble of checking if actual (any non-empty)
# line has neighboring line below.

# %h hash and $m hold seen states and maximum fill percentage for current board,
# they are initialized to undef i.e empty and 0.

(@a,%h,$m)=('',map<>=~y/ox\n/\0!/rd,1..$_);

# /
# Then do required number of evolutions:

for$n(0..10){

# Stringify board state, i.e. concatenate lines with spaces ($") as separators.
# Calculate fill percentage - divide number of '!' by number of non-spaces. 
# Note: using $. magick variable automatically takes care of rounding.
# Construct output string. It's not used if loop gets to next iteration. 
# Check if current state was already seen (at the same time add it to %h) 
# and if fill percentage is 0.

$_="Round $n: "
    .($h{$_="@a"}++?repetition:(($.=100*y/!///y/ //c)<$m?$.:$m=$.)?giveup:empty)
    .", $m percent maximum-fill\n";

# /
# Next is funny: if output string contains 'g' (of 'giveup' word), then evolve 
# further, otherwise break-out of the loop.

    @a=/g/
        ?map{

# Do evolution round. Act of evolution for a given line is none other than 
# XOR-ing 4 strings: itself shifted right, itself shifted left, line above, line 
# below. Result of this operation is truncated to original length using bitwise '&'. 
# Note, when shifting string right or left we prepend (append) not an ascii-0, 
# but 'P' character. It's shorter, and 4th and 6th bits will be annihilated anyway.

            $_=$a[$i=$_];
            y//!/cr
            &(s/.//r.P
            ^P.s/.$//r
            ^$a[$i+1]
            ^$a[$i-1])
        }0..$#a
        :last
}

Wow, một giải pháp quá nhanh. Tôi ngạc nhiên. Vì tôi không quen với PERL (mặc dù tôi đã cài đặt nó), làm cách nào để bắt đầu tập lệnh của bạn với dữ liệu đầu vào của tôi?
plocks

2
@DevanLoper, ví dụ: perl -p x.pl < input.txtnếu dữ liệu nằm trong một tệp hoặc perl -p x.pldòng cấp dữ liệu theo từng dòng để kiểm tra mục nhập đơn (chấm dứt bằng ctrl-D( ctrl-Z)). Hãy nhớ kiểm tra perl của bạn là 5.014hoặc mới hơn.
dùng2846289

Cảm ơn VadimR, bây giờ nó chạy. Nhưng tôi có một kết quả khác nhau trong hai dòng liên quan đến tỷ lệ phần trăm in. Nhưng đó có thể là lỗi làm tròn.
plocks

1
@DevanLoper, xin lỗi, đó là lỗi của tôi, phần trăm được lấy từ lần lặp trước. Tôi sẽ sửa nó sớm thôi.
dùng2846289

1
Đã sửa lỗi, + một số byte bị vứt đi. Kết quả xét nghiệm phù hợp với những người từ trang web được liên kết. Về mặt kỹ thuật, 11 vòng được chạy, nhưng trạng thái của vòng cuối không được kiểm tra cũng như không được sử dụng. Đó là tất cả cho ngắn gọn. Tôi đặt điều kiện phá vỡ vòng lặp trong đầu để bắt 1 \n ođầu vào.
dùng2846289

3

C # - 1164 ký tự

Đây là lần đầu tiên tôi tham gia vào môn đánh gôn, vì vậy hãy nuông chiều ;-)

Tôi biết, tôi còn lâu mới có kết quả tốt nhất - thực sự tuyệt vời!

Nhưng tôi nghĩ tôi sẽ chia sẻ giải pháp của mình trong C #.

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;class Program{static void Main(string[] args){new WebClient().DownloadFile("http://mc.capgemini.de/challenge/in.txt",@"D:\in.txt");var a=File.ReadAllLines(@"D:\in.txt");int l=0;while(l<a.Length){int n=Int32.Parse(a[l]);var b=a.Skip(l+1).Take(n).ToArray();var f=new List<string[]>{b};var d=0;string g=null;while(d<10){var s=f.Last();if(s.All(e=>e.All(c=>c=='o'))){g="empty";break;}var h=new string[n];for(int r=0;r<n;r++){var k="";for(int c=0;c<b[r].Length;c++){int x=0;try{if(s[r][c-1]=='x')x++;}catch{}try{if(s[r][c+1]=='x')x++;}catch{}try{if(s[r-1][c]=='x')x++;}catch{}try{if(s[r+1][c]=='x')x++;}catch{}k+=((x%2)==1)?'x':'o';}h[r]=k;}d++;f.Add(h);var w=false;for(int i=0;i<f.Count-1;i++){var m=f[i];if (!h.Where((t,y)=>t!=m[y]).Any())w=true;}if(w){g="repetition";break;}}if(d==10&&g==null)g="giveup";File.AppendAllLines(@"D:\out.txt",new[]{string.Format("Round {0}: {1}, {2} percent maximum-fill",d,g,f.Select(z=>{int t=0;int x=0;foreach(var c in z.SelectMany(s=>s)){t++;if(c=='x')x++;}return(int)Math.Floor((double)x/t*100);}).Concat(new[]{0}).Max())});l=l+n+1;}}}

Chỉ có các lệnh sử dụng đã đếm được 97 ký tự - vì vậy tôi nghĩ rằng sẽ khá khó để đạt được phần còn lại trong vòng dưới 200 ký tự.

Đó là một cách tiếp cận khá lặp đi lặp lại, sử dụng LINQ ở nhiều nơi. Tôi cũng bao gồm việc tải xuống tệp đầu vào và ghi tệp đầu ra vào mã.

Đây là một phiên bản dễ đọc hơn một chút:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        // Download the file
        new WebClient().DownloadFile("http://mc.capgemini.de/challenge/in.txt", @"D:\in.txt");
        // Read of lines of downloaded file
        var a = File.ReadAllLines(@"D:\in.txt");
        // Line index in input file
        int l = 0;
        while (l < a.Length)
        {
            // Parse number of rows to take
            int n = Int32.Parse(a[l]);

            // Take the n rows
            var b = a.Skip(l + 1).Take(n).ToArray();
            var f = new List<string[]> { b };
            var d = 0;
            string g = null;
            while (d < 10)
            {
                // Last state consists only of o's? -> 
                var s = f.Last();
                if (s.All(e => e.All(c => c == 'o')))
                {
                    g = "empty";
                    break;
                }
                // In h we will build up the new state
                var h = new string[n];
                // Loop through all rows of initial state
                for (int r = 0; r < n; r++)
                {
                    // This is our new row we will build up for the current state
                    var k = "";
                    // Count number of orthogonal adjacent x's
                    // And catch potential OutOfRangeExceptions
                    for (int c = 0; c < b[r].Length; c++)
                    {
                        int x = 0;
                        try { if (s[r][c - 1] == 'x') x++; }
                        catch { }
                        try { if (s[r][c + 1] == 'x') x++; }
                        catch { }
                        try { if (s[r - 1][c] == 'x') x++; }
                        catch { }
                        try { if (s[r + 1][c] == 'x') x++; }
                        catch { }
                        // Is number of adjacent x's odd? -> character will be 'x'
                        // otherwise -> 'o'
                        k += ((x % 2) == 1) ? 'x' : 'o';
                    }
                    // Add the new row to the current state
                    h[r] = k;
                }
                // Increase round count
                d++;
                // Add the new state to our state collection
                f.Add(h);
                // Now check, whether it is a repetition by comparing the last state (h) with all other states
                bool w = false;
                for (int i = 0; i < f.Count - 1; i++)
                {
                    var m = f[i];
                    if (!h.Where((t, y) => t != m[y]).Any())
                        w = true;
                }
                if (w)
                {
                    g = "repetition";
                    break;
                }
            }
            // Check whether we reached maximum AND the last round wasn't a repetition
            if (d == 10 && g == null)
                g = "giveup";
            // Now we append the final output row to our text file
            File.AppendAllLines(@"D:\out.txt",
                new[]
                    {
                        string.Format("Round {0}: {1}, {2} percent maximum-fill",
                        d,
                        g,
                        // Here we select all rates of x's per state
                        // and then grab the maximum of those rates
                        f.Select(z =>
                            {
                                int t=0;
                                int x=0;
                                foreach (char c in z.SelectMany(s => s))
                                {
                                    t++;
                                    if(c=='x')
                                        x++;
                                }
                                return (int) Math.Floor((double) x / t *100);
                            }).Concat(new[] {0}).Max())
                    });
            // finally we shift our index to the next (expected) number n in the input file
            l = l + n + 1;
        }
    }
}

1
Ngắn, ngắn hơn, Giải pháp của Ben. Bạn đã tạo ra một vi mô của một giải pháp suy nghĩ theo thuật ngữ C # ...
Paul Facklam

2

J - 275 char

Ồ, tất cả các thông số kỹ thuật I / O này! Thật là một số điểm đáng xấu hổ cho J, cuối cùng. Đưa đầu vào vào STDIN bằng một dòng mới, và giả sử không có bất kỳ trả về vận chuyển nào ( \r) trong đầu vào. Đây là kết quả của việc áp dụng nó cho tệp đầu vào mẫu trong câu hỏi.

stdout;,&LF&.>}:(".@{~&0(('Round ',":@(#->/@t),': ',(empty`repetition`giveup{::~2<.#.@t=.11&=@#,0={:),', ',' percent maximum-fill',~0":>./)@(100*1&=%&(+/"1)_&~:)@,.@(a=:(a@,`[@.(e.~+.10<#@[)(_*_&=)+[:~:/((,-)(,:|.)0 1)|.!.0=&1){:)@,:@('ox'&i.^_:)@{.;$: ::]@}.)}.)];._2[1!:1]3

Ungolfed: (Tôi có thể thêm một lời giải thích kỹ lưỡng và thân thiện hơn với J-newbie sau này.)

input   =: ];._2 [ 1!:1]3
convert =: 'ox'&i. ^ _:               NB. 'x'=>1  'o'=>0  else=>infinity
spread  =: ((,-)(,:|.)0 1) |.!.0 =&1  NB. x spreading outwards
cover   =: (_*_&=) + [: ~:/ spread    NB. collecting x`s and removing tiles not on board
iterate =: (iterate@, ` [ @. (e.~ +. 10<#@[) cover) {:
percent =: 100 * 1&= %&(+/"1) _&~:    NB. percentage of x at each step
max     =: 0 ": >./
stat    =: 11&=@# , 0={:              NB. information about the simulation
ending  =: empty`repetition`giveup {::~ 2 <. #.@stat   NB. how simulation ended
round   =: ": @ (# - >/@stat)         NB. round number
format  =: 'Round ', round, ': ', ending, ', ', ' percent maximum-fill',~ max
evolvex =: format @ percent@,. @ iterate@,: @ convert
joinln  =: ,&LF &.>
nlines  =: ". @ {~&0
remain  =: }.
stdout ; joinln }: (nlines (evolvex@{. ; $: ::]@}.) remain) input

Phần này $:làm cho phần thân chính lặp lại trên đầu vào (một dạng bất tiện khủng khiếp cho J để phân tích cú pháp), áp dụng @chuỗi daisy trên mỗi phần. nlinestìm số dòng cho bảng tiếp theo.

Hành động trên mỗi bảng ( evolvex) là gọn gàng: iterate(được gọi atrong golf) xây dựng một danh sách mỗi lần lặp của mô phỏng cho đến khi chúng ta nhấn một cái gì đó nhìn thấy trước hoặc quá nhiều bước. Sau đó percent@,.tính tỷ lệ phần trăm hình vuông được điền trong mỗi kết quả vàformat chạy một số thống kê ( statđược gọi là ttrong golf) để tìm ra cách mô phỏng kết thúc, phần trăm nào là lớn nhất, v.v., trước khi định dạng tất cả số đó thành một chuỗi.

Cuối cùng, xử }:lý một số rác trước khi ; joinlnnối tất cả các đầu ra của bảng riêng lẻ thành một chuỗi được phân tách bằng dòng mới.


Xin chào thuật toán, bạn có thể vui lòng cung cấp hướng dẫn về cách khởi động tập lệnh của bạn từ dòng lệnh với tệp .txt làm tham số đầu vào không? Cảm ơn!
plocks

1
@DevanLoper Điều đó nhắc nhở tôi, tôi đã quên làm cho nó xuất ra thiết bị xuất chuẩn; thêm mà sửa. Nó nên hoạt động theo cách tiêu chuẩn bây giờ : jconsole golf.ijs < input.txt.
thuật toán

Cảm ơn thông tin, nhưng nó vẫn không in ra bất kỳ đầu ra nào cho tôi, ngay cả khi mã của bạn đã thay đổi.
plocks

@DevanLoper Vấn đề dường như là việc tôi sử dụng vlàm tên, vì bất kỳ lý do gì đều không được phép trong các tập lệnh. (Tôi đã chạy đoạn mã trong REPL.) Thay đổi nó để axuất hiện để hoạt động.
thuật toán

@ achoshark Đó có thể là tôi, nhưng tôi vẫn không thể lấy nó ra để in cho tôi bất cứ điều gì.
plocks
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.