불로 그것을 죽이다


30

면책 조항 :이 질문에서 들었던 이야기는 전적으로 허구이며 소개를 제공하기위한 목적으로 만 고안되었습니다.

나는 사악한 농부이며, 내 지역에서 밀의 가격을 높이기 위해 내 주변의 모든 농부들의 들판을 태우기로 결정했습니다. 나는 들판이 화염에 올라가는 것을 정말로보고 싶습니다 (그래서 나는 악한 웃음을 사용하고 기쁨과 함께 내 손을 문지르는 것이 가능합니다). 나를 위해 소각했다.

당신의 작업 :

필드를 입력으로 받아 전체 필드가 ​​재가 될 때까지 레코딩 단계를 반환하는 프로그램 또는 함수를 작성하십시오. 화재가 발생한 필드의 특정 부분은 불꽃의 강도를 나타내는 정수로 표시됩니다. 화재는 "1"에서 시작하여 "2"로 이동 한 다음 "3"으로 이동합니다. 불이 "4"에 도달하면, 불에 가연성 인 인접한 (직사각형이 아닌) 인접한 지역을 잡습니다. "8"에 도달하면 다음 반복에서 소각되고 "A"로 표시되는 재로 바뀝니다. 영역이 아직 불에 닿지 않은 경우 "0"으로 표시됩니다. 예를 들어 필드가 다음과 같은 경우

100
000

프로그램은 이것을 출력해야합니다 :

100
000

200
000

300
000

410
100

520
200

630
300

741
410

852
520

A63
630

A74
741

A85
852

AA6
A63

AA7
A74

AA8
A85

AAA
AA6

AAA
AA7

AAA
AA8

AAA
AAA

원하는 경우 위의 기호를 일관되고 서로 구별되는 한 선택한 기호 세트로 바꿀 수 있습니다.

입력:

위와 같이 줄 바꿈 된 문자열과 같은 표준 형식의 필드 시작 위치입니다.

산출:

배열로 또는 일부 문자로 구분 된 문자열로 구울 때마다 모든 반복의 필드입니다.

테스트 사례 :

0301
000A
555
 |
 v
0301
000A
555

1412
010A
666

2523
020A
777

3634
030A
888

4745
141A
AAA

5856
252A
AAA

6A67
363A
AAA

7A78
474A
AAA

8A8A
585A
AAA

AAAA
6A6A
AAA

AAAA
7A7A
AAA

AAAA
8A8A
AAA

AAAA
AAAA
AAA

채점 :

이것은 이며 바이트 단위로 가장 낮은 점수를 얻습니다!


1
모양이 얼마나 다를 수 있습니까? 직사각형이 아닌 부품은 항상 오른쪽 모서리에 "구멍"이 있습니까? 아니면 필드에 공간이있을 수 있습니까?
PurkkaKoodari

3
따라서 4를 치는 것은 인접한 사각형에서 화재를 시작하지만 4 이상에서 시작하는 것은 그렇지 않습니까? 그것은 현실적이지 않습니다.
laszlok

14
"면책 조항 :이 질문에서 언급 된 이야기는 전적으로 허구이며 소개를 제공하기위한 목적으로 만 개발되었습니다." -> "나는 악한 일을하고 싶은 악한 농부"입니다. 매우 영리한. 아무도 당신 에게 불타고있는 분야를 관련시키지 않을 것 입니다.
J_F_B_M

3
초기 재가 필드를 두 개로 분리하여 그 일부가 타지 않도록 할 수 있습니까?
aschepler

9
4 이상으로 퍼지지 않는 화재가 너무 화 나면 1-4는 화재 강도이며 5-A는 화재를 나타냅니다.
Jeremy Weirich

답변:


1

APL (Dyalog) , 52 바이트 *

⎕IO←0많은 시스템에서 기본값으로 가정 합니다. 빈 슬롯은 0, 비 연소는 1, 새로운 화재는 2, 화재는 5, 재는 10을 사용합니다. 입력은 3x3 이상이어야합니다. 이는 추가 행과 열이 0으로 채워질 수 있으므로 문제가되지 않습니다 (OP 형식의 공백).

{}{1=c4r←,⍵:1+4r/⍨9⍴⍳210cc}⌺3 3⍣{⍺≡⎕←⍵}

온라인으로 사용해보십시오!

내 형식은 정확성을 확인하기 어렵 기 때문에 여기 에는 사전 및 사후 처리가 추가되어 OP 형식으로 변환하는 버전이 있습니다.

⍣{… 다음 } 까지 반복하십시오.

 다음 세대

 ~와 동일하다

⎕←⍵ 현재 세대, 출력

{}⌺3 3 각 셀을이 기능을 무어 인근에 적용한 결과로 바꿉니다.

 ,⍵ 논증을 풀고 (평평하게); 구 요소 목록을 제공합니다

r←r에  할당

4⊃ 네 번째 요소를 선택하십시오. 중심, 즉 원래 셀 값

c←c에  할당

1= 그것과 같은가요?

: 그렇다면 :

  ⍳2 처음부터 ntegers 까지; 0 1

  9⍴R 길이 아홉 eshape; 0 1 0 10 10

  r/⍨ 이를 사용하여 r 을 필터링 하십시오 (직교 이웃 만 가져옵니다)

  4∊ 네 사람입니까? (즉, 다음 세대에 다섯이 있을까요?)

  1+ 하나를 추가하십시오; 불이 붙지 않은 경우 1 또는 불이 붙은 경우 2

 그렇지 않은 경우 (예 : 현재 값이 0 또는 ≥ 2)

  ×cc  의 부호

  c+c 더하기 (즉, 불이 붙으면 1 씩 증가)

  10⌊ 최소 10 이상 (재가 타지 않기 때문에)


* Dyalog Classic에서는 ⎕U233A 대신을 사용 합니다 .


Small, but doesn't deal with board changes. Stuck to a 3x3.
Suamere

@Suamere What? The board doesn't change size, does it?
Adám

Sorry, I wasn't clear. Your answer is awesome, but I am of the understanding that the solution should allow for a variable sized board which may or may not have gaps "on the right". Gaps in the middle are not required to be handled. "On the Right" seems to mean a size-15 board would be built as a 4x4, except the right-most bottom piece is missing. And a size-8 board would be built as a 3x3, except the right-most bottom piece is missing, etc. That's how I read the challenge requirements. Your answer is currently the shortest, but only works with a 3x3.
Suamere

@Suamere OP clearly states that input is 2D. I take input as a numeric matrix and allow "empty" slots anywhere, in the form of zeros. While I require the input to be minimum 3×3 (OP allowed this) I accept input of any larger size. In fact, if you click the here link, you'll see that my second example case is 2×3 with the bottom right cell empty.
Adám

Gotcha. Not sure why, but the Try It Here link has issues (Probably my fault), though your new formatted link works well. E.G.: fire '0A000\n0A0A0\n0A0A0\n000A1' works perfect on the formatted one, but I can't get an equivalent to work with the first link. I'm probably doing something wrong. This doesn't work for me: f ↑(0 0 0)(0 1 0)(0 0 0)
Suamere

15

Python 3, 232 bytes

def f(j):
 k=[[int(min(9,j[x][y]+(j[x][y]>0)or 3in(lambda k,x,y:[k[i][j]for i,j in[[x-1,y],[x+1,y],[x,y-1],[x,y+1]]if(-1<i<len(k))and(-1<j<len(k[i]))])(j,x,y)))for y in range(len(j[x]))]for x in range(len(j))]
 if k!=j:print(k);f(k)

Try it online!

-3 bytes thanks to officialaimm by merging the other lambda into f (looks messy but saves bytes and that's all we care about)
-8 bytes thanks to Mr. Xoder
-26 bytes thanks to ovs
-6 bytes thanks to ppperry


How do I add a blank space, like that in the example?
tuskiomi

10

JavaScript (ES6), 217 210 207 204 193 192 190 bytes

Saved 2 bytes thanks to @Shaggy's suggestion of using 9 as A.

f=F=>[F,...(t=[],y=0,g=x=>(r=F[y])?(x||(t[y]=[]),r[x]+1)?(t[y][x]=r[x]<8?r[x]+(r[x]>0|[r[x-1],r[x+1],F[y-1]&&F[y-1][x],F[y+1]&&F[y+1][x]].includes(3)):9,g(x+1)):g(0,y++):t)(0)+""!=F?f(t):[]]

// test code
test=s=>{
  var test = document.querySelector("#in").value.split`\n`.map(e => Array.from(e).map(e => parseInt(e)));
  var out = f(test);
  document.querySelector("#out").innerHTML = out.map(e => e.map(e => e.join``).join`\n`).join`\n\n`;
};window.addEventListener("load",test);
<textarea id="in" oninput="test()">0301&#10;0009&#10;555</textarea><pre id="out"></pre>

Uses 9 instead of A. Input as a 2D array of integers. Output as an array of such arrays.


Could you save anything by using 9 instead of A?
Shaggy

7

Simulating the World (in Emoji), 1407 bytes?

Don't you love using an explorable explanation as a programming language? The downside to this is there's usually not a very well defined program, so in this case, I'm using the JSON it exports. (if you have any better ideas, let me know)

{"meta":{"description":"","draw":1,"fps":1,"play":true},"states":[{"id":0,"icon":"0","name":"","actions":[{"sign":">=","num":1,"stateID":"4","actions":[{"stateID":"1","type":"go_to_state"}],"type":"if_neighbor"}],"description":""},{"id":1,"icon":"1","name":"","description":"","actions":[{"stateID":"2","type":"go_to_state"}]},{"id":2,"icon":"2","name":"","description":"","actions":[{"stateID":"3","type":"go_to_state"}]},{"id":3,"icon":"3","name":"","description":"","actions":[{"stateID":"4","type":"go_to_state"}]},{"id":4,"icon":"4","name":"","description":"","actions":[{"stateID":"5","type":"go_to_state"}]},{"id":5,"icon":"5","name":"","description":"","actions":[{"stateID":"6","type":"go_to_state"}]},{"id":6,"icon":"6","name":"","description":"","actions":[{"stateID":"7","type":"go_to_state"}]},{"id":7,"icon":"7","name":"","description":"","actions":[{"stateID":"8","type":"go_to_state"}]},{"id":8,"icon":"8","name":"","description":"","actions":[{"stateID":"9","type":"go_to_state"}]},{"id":9,"icon":"A","name":"","description":"","actions":[]}],"world":{"update":"simultaneous","neighborhood":"neumann","proportions":[{"stateID":0,"parts":100},{"stateID":1,"parts":0},{"stateID":2,"parts":0},{"stateID":3,"parts":0},{"stateID":4,"parts":0},{"stateID":5,"parts":0},{"stateID":6,"parts":0},{"stateID":7,"parts":0},{"stateID":8,"parts":0},{"stateID":9,"parts":0}],"size":{"width":9,"height":9}}}

Try it here or here:

<iframe width="100%" height="450" src="http://ncase.me/simulating/model/?remote=-Kr2X939XcFwKAunEaMK" frameborder="0"></iframe>


6

Retina, 103 96 88 bytes

^
¶
;{:`

T`0d`d
(?<=¶(.)*)0(?=4|.*¶(?<-1>.)*(?(1)_)4|(?<=40|¶(?(1)_)(?<-1>.)*4.*¶.*))
1

Try it online! Uses 9 for ash; this can be changed at a cost of 4 bytes using T`1-8`2-8A. Edit: Saved 6 bytes thanks to @MartinEnder. Explanation:

^
¶

Add a separator so that the outputs don't run into each other. (Also helps when matching below.)

;{:`

Don't print the final state (which is the same as the previous state which has already been printed). Repeat until the pass does not change the state. Print the current state before each pass.

T`0d`d

Advance the intensity of all the fire.

(?<=¶(.)*)0(?=4|.*¶(?<-1>.)*(?(1)_)4|(?<=40|¶(?(1)_)(?<-1>.)*4.*¶.*))
1

Light unlit fields as appropriate. Sub-explanation:

(?<=¶(.)*)

Measure the column number of this unlit field.

0

Match the unlit field.

(?=4

Look for a suitable field to the right.

  |.*¶(?<-1>.)*(?(1)_)4

Look for a suitable field in the same column (using a balancing group) in the line below. Note that if the input could be guaranteed rectangular then this could be simplified to |.*¶(?>(?<-1>.)*)4 for a saving of 3 bytes.

  |(?<=40

Look for a suitable field to the left. (Since we're looking from the right-hand side of the field, we see the unlit field as well.)

      |¶(?(1)_)(?<-1>.)*4.*¶.*))

Look for a suitable field in the same column in the line above. Since this is a lookbehind and therefore a right-to-left match, the balancing group condition has to appear before the columns that have been matched by the balancing group.


5

Perl 5, 365 bytes

@a=map{[/./g]}<>;do{say@$_ for@a;say;my@n=();for$r(0..$#a){$l=$#{$a[$r]};for(0..$l){$t=$a[$r][$_];$n[$r][$_]=($q=$n[$r][$_])>$t?$q:$t==9?9:$t?++$t:0;if($t==4){$n[$r-1][$_]||=1if$r&&$_<$#{$a[$r-1]};$n[$r+1][$_]||=1if$r<$#a&&$_<$#{$a[$r+1]};$n[$r][$_-1]||=1if$_;$n[$r][$_+1]||=1if$_<$l}}}$d=0;for$r(0..$#a){$d||=$a[$r][$_]!=$n[$r++][$_]for 0..$#{$a[$r]}}@a=@n}while$d

Try it online!

Uses '9' instead of 'A' to indicated a burned out location.

Explained

@a=map{[/./g]}<>;   # split input into a 2-D array

do{
say@$_ for@a;say;   # output the current state
my@n=();            # holds the next iteration as it's created
for$r(0..$#a){      # loop through rows
  $l=$#{$a[$r]};    # holder for the length of this row
  for(0..$l){
    $t=$a[$r][$_];  # temporary holder for current value
    $n[$r][$_]=($q=$n[$r][$_])>$t?$q:$t==9?9:$t?++$t:0; #update next iteration
    if($t==4){      # ignite the surrounding area if appropriate
      $n[$r-1][$_]||=1if$r&&$_<$#{$a[$r-1]};
      $n[$r+1][$_]||=1if$r<$#a&&$_<$#{$a[$r+1]};
      $n[$r][$_-1]||=1if$_;
      $n[$r][$_+1]||=1if$_<$l
    }
  }
}
$d=0;              # determine if this generation is different than the previous
for$r(0..$#a){$d||=$a[$r][$_]!=$n[$r++][$_]for 0..$#{$a[$r]}}
@a=@n              # replace master with new generation
}while$d

4

Haskell, 162 bytes

import Data.List
i x|x<'9'=succ x|1<2=x
f('3':'@':r)="30"++f r
f(x:r)=x:f r
f e=e
a%b=a.b.a.b
g=map(i<$>).(transpose%map(reverse%f))
h x|y<-g x,y/=x=x:h y|1<3=[x]

Try it online! Usage: h takes a field as a list of lines and returns a list of fields. An unburned field is indicated by @ and ash by 9, the different fires are the digits 1 to 8.

  • f manages the spreading of the fire from left to right by replacing all unburned @ fields which are right to a burning 3 field with 0.
  • i increments each digit as long as it is less than 9.
  • g applies f to each line, then reverses the line, applies f again and reverses back. Then the list of of lines is transposed and again on each line and its reverse f is applied.
  • h applies g to the input until it no longer changes and collects the results.

Fails on input ""@3@1\n@@@9\n555@@@@@@@@@@@@@@@@@@@", as for some reason the long line of @s switches to the top line after the first iteration. Fixing that would be great, thanks!
Gryphon - Reinstate Monica

@Gryphon The displacement is caused by transposing the character matrix. It works if the other lines are brought to the same length with some character which represents neither a field, fire or ash, e.g. _. If this is not acceptable then I'm afraid I'd have to delete the answer, because it's centered around the usage of transpose and I don't see a way to easily fix it without introducing tons of bytes.
Laikoni

4

C (gcc), 308 305 299 297 295 291 bytes

#define F B[i][v]
m(A,B,k,y,m,U,i,v)char**B;{do{for(i=k=y=0;i<A;puts(""),++i)for(v=0;v<(m=strlen(B[i]));F=(U=F)>48&&F<56?F+1:F>55?65:(i+1<A&&B[i+1][v]==51||v+1<m&&B[i][v+1]==51||v-1>-1&&B[i][v-1]==52||i-1>-1&&B[i-1][v]==52)&&U<49?49:F,putchar(U),k+=U<49||U>64,++y,++v);puts("");}while(k-y);}

This program defines a function which takes two inputs, a pointer to an array of strings preceded by its length, as allowed by this I/O default. Outputs to STDOUT with a trailing newline.

Try it online!


Runs forever on input 80.
aschepler

@aschepler Sorry! I assumed that all the characters must turn into As, but apparently I assumed wrong. Anyways, thanks for the information. It's fixed now.
R. Kap


@GiacomoGarabello I can't believe I forgot about that tactic. Thanks for reminding me! :)
R. Kap

4

Octave, 72 69 bytes

a=input(''),do++a(a&a<9);a+=imdilate(a==4,~(z=-1:1)|~z')&~a,until a>8

The input is taken as a 2D array of numbers, and empty spots marked with Inf. 'A' has been replaced with 9. Intermediate results (as array of numbers) implicitly printed .

Try it online!

Explanation:

In a loop the function imdilate (morphological image dilation) from the image package is used to simulate fire propagation.


1
This works with boards of all sizes, and even with a bunch of holes. E.G.: [0 Inf 0 0 0;0 Inf 0 Inf 0;0 Inf 0 Inf 0;0 0 0 Inf 1] - Very Nice
Suamere

1

Python 2, 325 Bytes

def f(x):
 while{i for m in x for i in m}>{9,''}:
    yield x;i=0
    for l in x:
     j=0
     for c in l:
        if 0<c<9:x[i][j]+=1
        j+=1
     i+=1
    i=0
    for l in x:
     j=0
     for c in l:
        if c==0 and{1}&{x[k[1]][k[2]]==4for k in[y for y in[[i,i-1,j],[i<len(x)-1,i+1,j],[j,i,j-1],[j<len(l)-1,i,j+1]]if y[0]]}:x[i][j]+=1
        j+=1
     i+=1
 yield x

f takes the input as a 2D array of integers, and empty spots marked with ''. 'A' has been replaced with 9. The function outputs a generator of all the fields over time in the same format.

Try it online!


1

Octave, 212 bytes

function r(f)b=48;f-=b;c=-16;l=f~=-c;d=17;p=@(x)disp(char(x+b));g=@()disp(' ');p(f);g();while~all(any(f(:)==[0 d c],2))m=f>0&l;f(m)+=1;k=conv2(f==4,[0 1 0;1 0 1;0 1 0],'same')&~m&l;f(k)+=1;f(f>8&l)=d;p(f);g();end

To run, specify a character array such as:

f = ['0301','000A','555 '];

... then do:

r(f)

Explanation of the code to follow...

Try it online!

Note: I tried to run this code with tio.run, but I wasn't getting any output. I had to use another service.


Really just at the moment I want to post an octave answer you answer before me..
Michthan

1

PHP, 226 212 210 209 185 177 bytes

for($f=file(m);$f!=$g;print"
")for($g=$f,$y=0;$r=$f[$y++];)for($x=-1;~$c=$r[++$x];$f[$y-1][$x]=$c=="0"?strstr($g[$y-2][$x].$r[$x-1].$f[$y][$x].$r[$x+1],51)/3:min(++$c,9))echo$c;

takes input with a trailing newline from a file named m; 9 for ashes.

Run with -nr or try it online.


first approach: PHP 7.0, 209 bytes

for($f=$g=file(m);trim(join($f),"A
");print"
".join($f=$g))for($y=-1;(a&$c=$r[++$x])||$r=$f[$y-=$x=-1];)for(+$c&&$g[$y][$x]=++$c<9?$c:A,$a=4;$a--;$q=$p)$c-4|($w=&$g[$y+$p=[1,0,-1][$a]])[$q+=$x]!="0"||$w[$q]=1;

takes input with a trailing newline from a file named m.

Run with -nr or try it online.

PHP version notes (for old approach)

  • for PHP older than 7.0, replace everything after $c-4| with $g[$y+$p=[1,0,-1][$a]][$q+=$x]!="0"||$g[$y+$p][$q]=1;
  • for PHP older than 5.5, replace [1,0,-1][$a] with $a%2*~-($a&2)
  • for PHP newer than 7.0, replace a&$c with ""<$c, +$c with 0<$c and $c-4 with $c!=4

doesnt' work for other test cases...sandbox.onlinephpfunctions.com/code/…
g19fanatic

@g19fanatic fixed & golfed. thanks for noticing.
Titus

0

Octave, 419 312 bytes

M=input('') %take a matrix M as input
[a, b]=size(M); %size M for later
while mean(mean(M))!=9 %while the whole matrix M isn't ashes
M=M+round(M.^0.001); %if there is a nonzero int add 1 to it
for i=1:a %loop over all values of M
for j=1:b
if(M(i,j)==4) %if a value of 4 is found, ignite the zeros around it
if(M(max(i-1,1),j)==0) M(i-1,j)++;end
if(M(min(i+1,a),j)==0) M(i+1,j)++;end
if(M(i,max(j-1,1))==0) M(i,j-1)++;end      
if(M(i,min(j+1,b))==0) M(i,j+1)++;end
elseif(M(i,j)==10) M(i,j)--;
end
end
end
M %display the matrix
end

Try it online!

This is my version it works, so now I still need to golf it. I think it can be a lot shorter if I find a way to find the indices of the 4 in a matrix, but I don't know how.
PS: A is a 9 in my code.


2
Instead of endif endfor and endwhile you can write end
rahnema1

0

Stencil (-mode), 22 bytes

S8:'A'0::S⋄(3N)⌈S+s

Try it online!

Just like in the test input, use integers separated by spaces for 0-8, ' ' for blank and 'A' for A. Remember to add trailing blanks too.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.