Skip to content

Instantly share code, notes, and snippets.

@tompng
Last active September 24, 2017 06:34
Show Gist options
  • Save tompng/40506cfbc7e1a35ad4736374ca2c8d4e to your computer and use it in GitHub Desktop.
Save tompng/40506cfbc7e1a35ad4736374ca2c8d4e to your computer and use it in GitHub Desktop.
ruby hiroshima asciiart
a=->x{x.unpack('m')[0]};C=a.call %(#############################################################################################################################
RD1bMTE3MzE1MDAyNDQ4NSwyODY1MzY0NTQyNzU3LDM5NjQ5MTAyNDg3NDMsMjgzOTU5NjMxMTg0NSwyODM5NTk1NzYyOTgxXQpFPVsyNTUsNjYsMjU1LDY2LDY2LDY2LDY2XQpGPVs2Myw5OSwxNDksMjY1LDc2
MSwxMTkzLDMyNjksMjk0OSwyNDk5LDI2MTEsMzA4Nyw0MDk1XQpHPSVcICAuLi4sLCxYYGAuLi4sLCxYYGB+LS0tOztYJ35+LTo6OztYXn5+Ojo7KChYIn5+ISF8KV1YIiIhISEhIXtYIioqKjw8PE1cLnNwbGl0
KCdYJykKZGVmIGFhYiBhYWMseCx5CiAgeD49MCAmJiB5Pj0wICYmICgoYWFjW3ldfHwwKT4+eCkmMT09MQplbmQKSD1bQ10ucGFjaygnbScpLmRlbGV0ZSgiXG4iKQpAX3Q9QHQ9MApsb29wIGRvCiAgdyxoPTE2
MCw4MAogIEBfdCs9MC4wMQogIEB0PUBfdCUxCiAgYWFkPSgxLi5oKS5tYXB 7KDEu LncpL m1hc HsxfX 0KICB hYWU 9LT4oYWFjLGN4LGN5LGFhZixhYWc9YWFmLGFhaDogMC4xLGFhaTogZmFsc2
UpewogICAgY3csY2g9YWFjLm1heC5iaXRfbGVuZ3RoLGFhYy5zaXplCiA g I C A oW2N4LWFhZi8yLDBdLm1heC5yb3VuZC4uW2N4K2FhZi8yLHctMV0ubWlu
LnJvdW5kKS5lYWNoIGRvIHx4fAogICAgICAoW2N5LWFhZy8yLDBdLm1 heC 5yb3 VuZ C4u W2N5K2FhZy8yLGgtMV0ubWluLnJvdW5kKS5lYWNoIGRvIHx5fAogICA
gICAgIGl4PXgtY3grYWFmLzIKICAgICAgICBpeT15LWN5K2FhZy8y C iAgICAgICAgaX gsaXk9aXgtaXk r YWFmLzIsaXgraXktYWFnLzIgaWYgYWFpCiAgICAgICAgYWFkW3ldW
3hdKj1hYWggaWYgYWFiIGFhYyxpeCpjdy9hYWYsaXkqY2gvYWFn CiAgI CAgIGVuZAogIC AgZW5kCiAgfQo gIGFh ZVtELHcqNy80LShAdCp3KjMpLHcvNCx3KjMvMix3KjMvMTYsYWF
oOiAwXQogIGFhZVtGLHcvMix3LzYsdy8yLGFhaDogWzAsNi1A dCo4LDF dLnNvcnRbMV 0sYWFpOiB0c nVlXQog IGFhaj13LzIqKDQqKEB0KyhAdCkqKjIqMTYpKQogIGFhZVtFL
HcvMix3LzItYWFqLzIsYWFqLGFhaDogMF0KICBbWzAuMTIs MC4wOV0 sWzAuMD gsMC4xM V0sWzAu MTAsMC4wN11dLmVhY2hfd2l0aF9pbmRleCBkbyB8KGNjLGR
kKSxlZXwKICAgIHcudGltZXMubWFwe3x4eHwKICAgICAg d3k9MC4xKz AuMDUqZ WUr MC4 wNiooTWF0a C5zaW4oeHgqY2MtODAqZGQqQF90KStNYXRoLnNpbih4eC
pkZCs4MCpjYypAX3QpKQogICAgICAoWyh3LzItdyp3e S8yKS5mbG9vc iwwXS5 tYXguL mgtMSk uZWFja CBkbyB8eXl8CiAgICAgICAgYWFkW3l5XVt4eF09W2Fh
ZFt5eV1beHhdLTAuMzMsMC4yXS5tYXgKICAgICAgZ W5 kC iA gI CB 9C iAgZW5kCiAgJD48PCAiXGVbMTsxSCIKICBhYWQuZW
FjaCBkbyB8YWFrfAogICAgYWFrLmVhY2hfd2l0aF9 pb mR le Ht 8e Sx pf GFha1tpXT0xIGlmIHk+MX0KICBlbmQKICBhYWw9KG
gvMikudGltZXMubWFwIHt8eXwKICAgIHcudGltZXMub WFwe3x4fAogI CAgICBHWyhhY WRbMip 5KzFdW 3hdKjc uOSkuZmx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aXpl CiAgICA oW2N4LWFhZi 8yLDBdL m1heC5yb3VuZC4uW2N4K2FhZi8yLHctMV0ubWluLnJvdW5kKS5lYWNoIGRv
IHx4fAogICAgICAoW2N5LWFhZy8yLDBdLm1heC5yb3VuZC4uW2N5K2FhZy8yL GgtMV 0ubWluLnJvd W5kKS 5lYWNoIGRvIHx5fAogICAgICAgIGl4PXgtY3grYWFmLzIKICAgICAgICBpeT1
5LWN5K2FhZy8yCiAgICAgICAgaXgsaXk9aXgtaXkrYWFmLzIsaXgraXktYWFnLz I gaWYgYW F pCiAgICAgICAgYWFkW3ldW3hdKj1hYWggaWYgYWFiIGFhYyxpeCpjdy9hYWYsaX
kqY2gvYWFnCiAgICAgIGVuZAogICAgZW5kCiAgfQogIGFhZVtELHcqNy80LShAdCp 3Kj MpLHcvNCx3KjMvMix3KjMvMTYsYWFoOiAwXQogIGFhZVtGLHcvMix3LzYsdy8yLGF
haDogWzAsNi1AdCo4LDFdLnNvcnRbMV0sYWFpOiB0cnVlXQogIGFhaj13LzIqKDQqKE B0KyhAdCkqKjIqMTYpKQogIGFhZVtFLHcvMix3LzItYWFqLzIsYWFqLGFhaDogMF0KI
CBbWzAuMTIsMC4wOV0sWzAuMDgsMC4xMV0sWzAuMTAsMC4wN11dLmVhY2hfd2l0aF9pbm RleCBkbyB8KGNjLGRkKSxlZXwKICAgIHcudGltZXMubWFwe3x4eHwKICAgICAgd3k9MC4
|||||||||||||xKzAuMDUqZWUrMC4wNiooTWF0aC5zaW4oeHgqY2MtODAqZGQqQF90KStNY XRoLnNpbih4eCpkZCs4MCpjYyp||||||||||AX3QpKQogICAgICAoWyh3LzItdyp3eS8yKS
||||||||||||||||||5mbG9v||||||ciwwXS5tYXguLmgtMSkuZWFjaCBkbyB8eXl8CiAgICA gICAgYWFkW3l5XVt4eF09W|||||||||||||||||||||2FhZFt5eV1beHhdLTAuMzMsMC4yXS5
|||||||||||||||||||~~~|||||||||||||tYXgKICAgICAgZW5kCiAgICB9CiAgZW5kCiAgJD4 8PCAiXGVbMTsxSCIKIC||||||||||||||||||||||||||||||BhYWQuZWFjaCBkbyB8YWFrfAog
||||||||||||||||~~~~~~~~~~||||||||||||ICAgYWFrLmVhY2hfd2l0aF|||~~~~||||9pbmRl eHt8eSxpfGFha1tpX|||||||||||||||||||||||||||~~~~~~|||T0xIGlmIHk+MX0KICBlbmQKI
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|||||||||||CBhYWw9KGg||||||~~~~~~~~~~~~~~~~~|||||``vMikudGltZXM||||||||||||||||||||||||||~~~~~~~~~~~~~~~~~~~~ubWFwIHt8eXwKICAgIHcu
~~~~~~~~~~~`````~~~~~~~~~~~~~~~~~~~~~~~~~~~~|||||||||||~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~||||||||||||~~~~~~~~~````````~~~~~~~~~~~~~~~|dGltZXMubWFwe3x4f
~~~~~~~~~``````````~~~~~~~~~~~~~~~~~~~~~~~~~~~~||||||~~~~~~~~~~~~~~~~~~```````~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~````````````````````~~~~~~~~~~~||AogICAgICBHWy
~~~~~~```````````````~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~~~~~~~~~~~~```````````````~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`````````````````````````````~~~~~~~~~|hhYWRbM|||
~~~~```````````````````~~~~~~~~~~~~~~~~~~~~~~~~~~``````````````````````````````````~~~~~~~~~~~~~~~~~~~~~~~~~``````````````````````````````````````````````~~~~~~
~````````````````````````~~~~~~~~~~~~~~~~~~~~~~``````````````````````````````````````~~~~~~~~~~~~~~~~~~~~```````````````````````````````````````````````````````
````````````````````````````~~~~~~~~~~~~~~~~````````````````````````````````````````````~~~~~~~~~~~~~```````````````````````````````````````````````````````````
```````````````````````````````~~~~~~~~~~```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
);eval C#```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
def data2i data
data.lines.map(&:strip).reject(&:empty?).map do |s|
s.scan(/[#-]/).join.tr('-#', '01').reverse.to_i(2)
end
end
p data2i %(
#-#--#--##----##----##--#-#--#--#---#---#-
#-#--#--#-#--#--#--#----#-#--#--##-##--#-#
###--#--##---#--#---#---###--#--##-##--###
#-#--#--#-#--#--#----#--#-#--#--#-#-#--#-#
#-#--#--#-#---##---##---#-#--#--#-#-#--#-#
)
p data2i %(
########
-#----#-
########
-#----#-
-#----#-
-#----#-
-#----#-
)
p data2i %(
######
##---##
#-#-#--#
#--#----#
#--#####-#
#--#-#-#--#
#-#---##--##
#-#----###-#
##----###--#
##--##---#-#
####------##
############
)
CHARTABLE = [
' ...,,,',
'``...,,,',
'``~---;;',
"'~~-::;;",
'^~~::;((',
'"~~/!|)]',
'""//!11{',
'"**+<<tM'
]
C = DATA.read
noconvs = %w(loop def end do if puts sleep true false Math raise eval unpack)
constconvs,varconvs={a:'A',b:'B',c:'C'},{'aaa': 'aaa'}
convsget = ->convs,name{
convs[name] || convs[name] ||= convs.values.max&.next||'a'
}
C.gsub!(/[A-Z][A-Z]+/){|n|convsget[constconvs,n].upcase}
C.gsub!(/[^.a-z_A-Z][a-z][a-z][a-z_]+/){|n|
noconvs.include?(n[1..-1]) ? n : n[0]+convsget[varconvs,n[1..-1]]
}
C.gsub!(%r{ ?[,=>+*/-] ?}, &:strip)
C.gsub!("\n\n", "\n")
C+="\n__END__\n"
showerr = ->name{
varconvs.to_a.find{|a,b|b==name} || constconvs.to_a.find{|a,b|b==name}
}
eval C
__END__
HIROSHIMA = [1173150024485, 2865364542757, 3964910248743, 2839596311845, 2839595762981]
TORII = [255, 66, 255, 66, 66, 66, 66]
RUBY = [63, 99, 149, 265, 761, 1193, 3269, 2949, 2499, 2611, 3087, 4095]
CHARTABLE = %\ ...,,,X``...,,,X``~---;;X'~~-::;;X^~~::;((X"~~!!|)]X""!!!!!{X"***<<<M\.split('X')
def dataget data, x, y
x>=0 && y >= 0 && ((data[y]||0)>>x)&1 == 1
end
ENCCODE = [C].pack('m').delete("\n")
@_t = @t = 0
loop do
w,h=160,80
@_t += 0.01
@t = @_t%1
canvas = (1..h).map{(1..w).map{1}}
drawimage = ->(data, cx, cy, sizew, sizeh=sizew, color: 0.1, rot: false){
cw,ch = data.max.bit_length, data.size
([cx-sizew/2,0].max.round..[cx+sizew/2,w-1].min.round).each do |x|
([cy-sizeh/2,0].max.round..[cy+sizeh/2,h-1].min.round).each do |y|
ix=x-cx+sizew/2
iy=y-cy+sizeh/2
ix,iy = ix-iy+sizew/2, ix+iy-sizeh/2 if rot
canvas[y][x] *= color if dataget data, ix*cw/sizew, iy*ch/sizeh
end
end
}
drawimage[HIROSHIMA, w*7/4-(@t*w*3), w/4, w*3/2, w*3/16, color: 0]
drawimage[RUBY, w/2, w/6, w/2, color: [0,6-@t*8,1].sort[1], rot: true]
size=w/2*(4*(@t+(@t)**2*16))
drawimage[TORII, w/2, w/2-size/2, size, color: 0]
[[0.12, 0.09], [0.08, 0.11], [0.10, 0.07]].each_with_index do |(cc, dd), ee|
w.times.map{|xx|
wy = 0.1+0.05*ee+0.06*(Math.sin(xx*cc-80*dd*@_t)+Math.sin(xx*dd+80*cc*@_t))
([(w/2-w*wy/2).floor, 0].max..h-1).each do |yy|
canvas[yy][xx] = [canvas[yy][xx]-0.33, 0.2].max
end
}
end
$><< "\e[1;1H"
canvas.each do |line|
line.each_with_index{|y,i|line[i]=1 if y>1}
end
output = (h/2).times.map {|y|
w.times.map{|x|
CHARTABLE[(canvas[2*y+1][x]*7.9).floor][(canvas[2*y+1][x]*7.9).floor]
}.join
}.join "\n"
cnt=-1
decoder = %[a=->x{x.unpack('m')[0]};C=a.call %(]
puts decoder+'#'*(w-decoder.size)
puts output.gsub('M'){
ENCCODE[(cnt+=1)%ENCCODE.size]
}
puts ");eval C##{'`'*(w-9)}"
raise "#{ENCCODE.size-cnt}" if ENCCODE[cnt]
sleep 0.05
end
require 'pry'
def data2i data
data.lines.map(&:strip).reject(&:empty?).map do |s|
s.scan(/[#-]/).join.tr('-#', '01').reverse.to_i(2)
end
end
# [4876605653, 11654440277, 15950488791, 11453211989, 11453176149]
HIROSHIMA = data2i %(
#-#-#-##---##---##-#-#-#-#---#--#-
#-#-#-#-#-#--#-#---#-#-#-##-##-#-#
###-#-##--#--#--#--###-#-##-##-###
#-#-#-#-#-#--#---#-#-#-#-#-#-#-#-#
#-#-#-#-#--##--##--#-#-#-#-#-#-#-#
)
# [63, 18, 63, 18, 18, 18]
TORII = data2i %(
######
-#--#-
######
-#--#-
-#--#-
-#--#-
)
# [63, 99, 149, 265, 761, 1193, 3269, 2949, 2499, 2611, 3087, 4095]
RUBY = data2i %(
######
##---##
#-#-#--#
#--#----#
#--#####-#
#--#-#-#--#
#-#---##--##
#-#----###-#
##----###--#
##--##---#-#
####------##
############
)
CHARTABLE = [
'MMMMMM###TTTTTTT',
'QQBMMNW##TTTTTV*',
'QQQBBEK@PTTTVVV*',
'QQQmdE88P9VVVV**',
'QQQmdGDU0YVV77**',
'pQQmAbk65YY?7***',
'ppgAww443vv?7***',
'pggyysxcJv??7***',
'pggyaLojrt<<+**"',
'gggaauuj{11!//""',
'gggaauui])|!/~~"',
'ggaauui]((;::~~^',
"ggaauu](;;::-~~'",
'ggauu(;;;;---~``',
'gaau;;,,,,,...``',
'gau,,,,,,,,... '
]
def dataget data, x, y
x>=0 && y >= 0 && ((data[y]||0)>>x)&1 == 1
end
def canvas2aa canvas
w, h = canvas[0].size, canvas.size
canvas.each do |line|
line.each_with_index{|y,i|line[i]=1 if y>1}
end
(h/2).times.map {|y|
w.times.map{|x|
a=(canvas[2*y][x]*0xff).floor/16
b=(canvas[2*y+1][x]*0xff).floor/16
CHARTABLE[a][b]
}.join
}
end
class Scene
def initialize
@_t = @t = 0
end
def update
@_t += 0.01
@t = @_t%1
end
def show w=80, h=48
canvas = Array.new(h){Array.new(w){1}}
drawimage = ->(data, cx, cy, sizew, sizeh=sizew, color: 0.1, rot: false){
cw,ch = data.max.bit_length, data.size
([cx-sizew/2,0].max.round..[cx+sizew/2,w-1].min.round).each do |x|
([cy-sizeh/2,0].max.round..[cy+sizeh/2,h-1].min.round).each do |y|
ix=x-cx+sizew/2
iy=y-cy+sizeh/2
ix,iy = ix-iy+sizew/2, ix+iy-sizeh/2 if rot
canvas[y][x] *= color if dataget data, ix*cw/sizew, iy*ch/sizeh
end
end
}
drawimage.call HIROSHIMA, w*7/4-(@t*w*3), w/4, w*3/2, w*3/16
drawimage.call RUBY, w/2, w/6, w/2, color: [0,6-@t*8,1].sort[1], rot: true
size=w/2*(4*(@t+(@t)**2*16))
drawimage.call TORII, w/2, w/2-size/2, size
[[0.12, 0.09], [0.08, 0.11], [0.10, 0.07]].each_with_index do |(c, d), e|
w.times.map{|x|
wy = 0.1+0.05*e+0.06*(Math.sin(x*c-80*d*@_t)+Math.sin(x*d+80*c*@_t))
([(w/2-w*wy/2).floor, 0].max..h-1).each do |y|
canvas[y][x] = [canvas[y][x]-0.33, 0].max
end
}
end
$><< "\e[1;1H"
puts canvas2aa canvas
end
end
scene = Scene.new
loop{
scene.update
scene.show
sleep 0.05
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment