Talk:One-dimensional cellular automata: Difference between revisions

From Rosetta Code
Content added Content deleted
(Feeling that code compacity is important here, so I propose a different script to achieve this goal)
 
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
I propose a different script to achieve this goal as :
I propose a different script to achieve this goal as :
<pre>
#!/usr/local/bin/gawk -f
#!/usr/local/bin/gawk -f

# FONCTIONS DEFINIES PAR UTILISATEUR
# User defined functions
function ASCII_to_Binary(str_) {
function ASCII_to_Binary(str_) {
gsub("_","0",str_); gsub("@","1",str_)
gsub("_","0",str_); gsub("@","1",str_)
Line 19: Line 21:
return d == 1 ? 1 : 0
return d == 1 ? 1 : 0
}
}

# POUR CHAQUE LIGNE DU FICHIER EN ENTREE FAIRE
# For each line in input do
{
{
str_=$0
str_=$0
Line 38: Line 41:
} while (str_ != str_previous)
} while (str_ != str_previous)
}
}

# ACTIONS POST TRAITEMENT
Then :
END {
$ echo ".@@@.@@.@.@.@.@..@.." | awk -f automata.awk
}
0: .@@@.@@.@.@.@.@..@..
1: _@_@@@@@_@_@_@______
2: __@@___@@_@_@_______
3: __@@___@@@_@________
4: __@@___@_@@_________
5: __@@____@@@_________
6: __@@____@_@_________
7: __@@_____@__________
8: __@@________________
9: __@@________________
</pre>
--[[User:Poulteki|Poulteki]] ([[User talk:Poulteki|talk]]) 15:31, 15 November 2016 (UTC)

:Please sign your posts and try and fix formatting issues, thanks. --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 18:41, 17 January 2015 (UTC)

Latest revision as of 14:32, 16 November 2016

I propose a different script to achieve this goal as :

#!/usr/local/bin/gawk -f

# User defined functions
function ASCII_to_Binary(str_) {
	gsub("_","0",str_); gsub("@","1",str_)
	return str_
}

function Binary_to_ASCII(bit_) {
	gsub("0","_",bit_); gsub("1","@",bit_)
	return bit_
}

function automate(b1,b2,b3) {
	a = and(b1,b2,b3)
	b = or(b1,b2,b3)
	c = xor(b1,b2,b3)
	d = a + b + c
	return d == 1 ? 1 : 0
}

# For each line in input do
{
str_=$0
gen=0
taille=length(str_)
print "0: " str_
do {
	gen ? str_previous=str_ : str_previous=""
	gen=gen + 1
	str_=ASCII_to_Binary(str_)
	split(str_,tab,"")
	str_=and(tab[1],tab[2])
	for (i=1; i<=taille-2; i++) {
		str_ = str_ automate(tab[i],tab[i+1],tab[i+2])
		}
	str_ = str_ and(tab[taille-1],tab[taille])
	print gen ": " Binary_to_ASCII(str_)
   } while (str_ != str_previous)
}

Then :
$ echo ".@@@.@@.@.@.@.@..@.." | awk -f automata.awk
0: .@@@.@@.@.@.@.@..@..
1: _@_@@@@@_@_@_@______
2: __@@___@@_@_@_______
3: __@@___@@@_@________
4: __@@___@_@@_________
5: __@@____@@@_________
6: __@@____@_@_________
7: __@@_____@__________
8: __@@________________
9: __@@________________

--Poulteki (talk) 15:31, 15 November 2016 (UTC)

Please sign your posts and try and fix formatting issues, thanks. --Paddy3118 (talk) 18:41, 17 January 2015 (UTC)