Penafian: Kisah yang diceritakan dalam pertanyaan ini sepenuhnya fiktif, dan diciptakan semata-mata untuk tujuan memberikan intro.
Saya seorang petani jahat, dan untuk menaikkan harga gandum di daerah saya, saya telah memutuskan untuk membakar ladang semua petani di sekitar saya. Saya benar-benar ingin melihat ladang terbakar (sehingga saya bisa menggunakan tawa jahat saya dan menggosok tangan saya bersama-sama dengan gembira), tetapi saya juga tidak ingin ketahuan menonton, jadi saya perlu Anda untuk mensimulasikan lapangan menjadi dibakar untukku.
Tugas Anda:
Tulis sebuah program atau fungsi yang mengambil sebagai input bidang, dan mengembalikan tahapan-tahapannya membakar sampai seluruh bidang menjadi abu. Bagian spesifik dari bidang yang terbakar diwakili oleh bilangan bulat yang mewakili intensitas nyala api. Api dimulai pada "1" dan bergerak ke "2" dan kemudian "3", dan seterusnya. Begitu api mencapai "4", api akan menangkap area yang berdekatan secara langsung (tidak diagonal) yang mudah terbakar. Setelah mencapai "8", ia terbakar pada iterasi berikutnya, dan berubah menjadi abu, diwakili oleh "A". Ketika suatu daerah belum disentuh oleh api, itu diwakili oleh "0". Misalnya, jika bidangnya terlihat seperti ini:
100
000
Program Anda harus menampilkan ini:
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
Jika mau, Anda dapat mengganti simbol di atas dengan seperangkat simbol yang Anda pilih, asalkan konsisten dan berbeda satu sama lain.
Memasukkan:
Posisi awal bidang, dalam bentuk standar apa pun, seperti string yang dibatasi baru seperti di atas.
Keluaran:
Bidang di setiap iterasi saat dibakar, baik sebagai array, atau sebagai string yang dibatasi oleh beberapa karakter.
Kasus uji:
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
Mencetak:
Ini adalah kode-golf , skor terendah dalam byte menang!
sumber
Jawaban:
APL (Dyalog) , 52 byte *
Mengasumsikan
⎕IO←0
yang default pada banyak sistem. Mengambil lapangan menggunakan 0 untuk slot kosong, 1 untuk bidang tidak terbakar, 2 untuk api baru, 5 untuk menyebarkan api, dan 10 untuk abu. Input harus minimal 3 × 3, yang tidak masalah karena baris dan kolom tambahan mungkin diisi dengan nol (spasi dalam format OP).Cobalah online!
Format saya membuatnya sulit untuk memeriksa kebenarannya, jadi di sini adalah versi dengan pemrosesan pra dan pasca yang ditambahkan untuk menerjemahkan dari dan ke format OP.
⍣{
...}
ulangi sampai:⍺
generasi penerus bangsa≡
identik dengan⎕←⍵
generasi saat ini, dikeluarkan{
...}⌺3 3
ganti setiap sel dengan hasil fungsi ini diterapkan pada lingkungan Moore:,⍵
ravel (meratakan) argumen; memberikan daftar sembilan elemenr←
tetapkan ke r4⊃
pilih elemen keempat; pusat, yaitu nilai sel aslic←
ditugaskan untuk c1=
Apakah sama dengan itu?:
jika demikian, maka:⍳2
pertama ke ɩ ntegers; 0 19⍴
r eshape sampai panjang sembilan; 0 1 0 1 0 1 0 1 0r/⍨
gunakan itu untuk menyaring r (ini hanya mendapat tetangga orthogonal)4∊
Apakah empat anggota itu? (yaitu apakah akan ada lima generasi berikutnya?)1+
tambahkan satu; 1 jika tidak terbakar atau 2 jika terbakar⋄
lain (yaitu nilai saat ini adalah 0 atau ≥ 2)×c
signum dari cc+
c plus itu (yaitu bertambah satu jika terbakar)10⌊
minimum of ten and that (as ash doesn't burn on)* In Dyalog Classic, using
⎕U233A
instead of⌺
.sumber
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)
Python 3, 232 bytes
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
sumber
JavaScript (ES6),
217210207204193192190 bytesSaved 2 bytes thanks to @Shaggy's suggestion of using
9
asA
.Uses
9
instead ofA
. Input as a 2D array of integers. Output as an array of such arrays.sumber
9
instead ofA
?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)
Try it here or here:
sumber
Retina,
1039688 bytesTry it online! Uses
9
for ash; this can be changed at a cost of 4 bytes usingT`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.
Advance the intensity of all the fire.
Light unlit fields as appropriate. Sub-explanation:
Measure the column number of this unlit field.
Match the unlit field.
Look for a suitable field to the right.
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.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.)
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.
sumber
Perl 5, 365 bytes
Try it online!
Uses '9' instead of 'A' to indicated a burned out location.
Explained
sumber
Haskell, 162 bytes
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 by9
, the different fires are the digits1
to8
.f
manages the spreading of the fire from left to right by replacing all unburned@
fields which are right to a burning3
field with0
.i
increments each digit as long as it is less than9
.g
appliesf
to each line, then reverses the line, appliesf
again and reverses back. Then the list of of lines is transposed and again on each line and its reversef
is applied.h
appliesg
to the input until it no longer changes and collects the results.sumber
_
. If this is not acceptable then I'm afraid I'd have to delete the answer, because it's centered around the usage oftranspose
and I don't see a way to easily fix it without introducing tons of bytes.C (gcc),
308305299297295291 bytesThis 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!
sumber
80
.A
s, but apparently I assumed wrong. Anyways, thanks for the information. It's fixed now.Octave,
7269 bytesThe input is taken as a 2D array of numbers, and empty spots marked with
Inf
.'A'
has been replaced with9
. 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.sumber
[0 Inf 0 0 0;0 Inf 0 Inf 0;0 Inf 0 Inf 0;0 0 0 Inf 1]
- Very NicePython 2, 325 Bytes
f
takes the input as a 2D array of integers, and empty spots marked with''
.'A'
has been replaced with9
. The function outputs a generator of all the fields over time in the same format.Try it online!
sumber
Octave, 212 bytes
To run, specify a character array such as:
... then do:
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.
sumber
PHP,
226 212 210 209 185177 bytestakes 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
takes input with a trailing newline from a file named
m
.Run with
-nr
or try it online.PHP version notes (for old approach)
$c-4|
with$g[$y+$p=[1,0,-1][$a]][$q+=$x]!="0"||$g[$y+$p][$q]=1;
[1,0,-1][$a]
with$a%2*~-($a&2)
a&$c
with""<$c
,+$c
with0<$c
and$c-4
with$c!=4
sumber
Octave,
419312 bytesTry 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.
sumber
endif
endfor
andendwhile
you can writeend
Stencil (
∊
-mode), 22 bytesTry it online!
Just like in the test input, use integers separated by spaces for
0
-8
,' '
for blank and'A'
forA
. Remember to add trailing blanks too.sumber