Promedio de una imagen


23

Aquí hay una imagen:

%%%%%%%%%%%%%
% Hello,    %
%    world! %
%%%%%%%%%%%%%

Pero es demasiado confuso para nuestros cerebros de tamaño insuficiente para calcular. Entonces lo promediamos así:

  1. Divídalo en 2 x 2 secciones. Si la imagen termina antes de que se complete una sección, imagine que hay espacios allí.

  2. Promedio de los valores de los caracteres ASCII en cada sección.

  3. Redondea este promedio y conviértelo a un carácter ASCII.

  4. Finalmente, reemplace todos los caracteres de la sección por el carácter promedio.

Repita esto para todas las secciones.

Entonces el promedio de la imagen de arriba se ve así:

$$>>II99######
$$>>II99######
$$##88KKGG####
$$##88KKGG####

Su tarea: escriba un programa que tome una imagen ASCII como entrada y genere su promedio.

Nota Los enteros se redondean por función floor(x+0.5)o función similar; en otras palabras, redondean mitades hacia arriba.


1
¿Qué sucede si el ancho es impar?
Leaky Nun

3
@KennyLau "Si la imagen termina antes de que se complete una sección, imagine que hay espacios allí". Creo que eso cubre el caso cuando el ancho es extraño;)
Katenkyo

¿Podemos suponer que la altura siempre será pareja?
FliiFe

2
@DenkerAffe No. Eso sería una trampa. : P

1
Solo para aclarar con el comportamiento esperado, ¿una imagen de 7x7 actuaría como una imagen de 8x8 con espacios en el borde inferior y derecho? Por lo tanto, nuestra salida también sería 8x8?
wnnmaw

Respuestas:


7

JavaScript (ES6), 159 bytes

document.write("<pre>"+(

// --- Solution ---
s=>s.replace(/./g,(c,i)=>(a=String.fromCharCode([t=0,1,l=s.search`
`+1,l+1].map(o=>t+=(n=s.charCodeAt(p=i+o-i%l%2-(i/l|0)%2*l))>32?n:32)|t/4+.5))+(++p%l?"":a))
// ----------------

)(`%%%%%%%%%%%%%
% Hello,    %
%    world! %
%%%%%%%%%%%%%`))

Toma una cadena multilínea como entrada.


Al ejecutarse en Firefox, esto produce resultados incorrectos.
Trebuchette

@Trebuchette Ah, interpreté mal la regla del espacio. Ya está arreglado.
user81655

4

MATL , 32 30 bytes

2thZCO32XEoYmYocGZy2/Xke2t3$Y"

La entrada es una matriz de caracteres 2D, con filas separadas por ;.

Pruébalo en línea!

Explicación

2th     % push array [2 2]
ZC      % take input implicitly. Arrange distinct 2x2 blocks as columns, padding with 0
O32XE   % replace 0 by 32 (space)
oYm     % convert to number. Take mean of each column
Yoc     % round. Convert to char
GZy     % size of input in the 2 dimensions
2/Xk    % divide each dimension by 2, and round up to account for the padding
e       % reshape into image with half original size in each dimension
2t3$Y"  % replicate by a factor of 2 in each dimension. Display implicitly


1

LUA, 382 376 367 353 348 bytes

r="\n"o=... n=o:find(r)-1
l=n+n%2
a=o:gsub(r,(n~=l and" "or"")..r).." "..(#o//(l+1)%2<1 and r..(" "):rep(l)or"")print(a:gsub("()([^\n])(.)",function(p,m,c)t=p//(l+1)%2==0return string.char(math.floor((m:byte()+c:byte()+(t and a:sub(p+l+1,p+l+1)or a:sub(p-l,p-l)):byte()+(t and a:sub(p+l+2,p+l+2)or a:sub(p-l-1,p-l-1)):byte())/4+.5)):rep(2)end).."")

Funciona en la línea de comando; acepta una cadena como el caso de prueba.


¿Te gustaría incluir una versión no golfista?
Leaky Nun

0

Ruby, 235 230 bytes

->i{i=i.split($/).map{|s|s.bytes+[s.size%2>0?32:0]}
w=i[0].size;h=i.size;h+=h%2;r=[[]]*h
(h/2).times{|y|y*=2
(w/2).times{|x|x*=2
c=((i[y][x,2]+(i[y+1]||[32]*w)[x,2]).inject(:+)/4.0).round.chr
r[y+1]=r[y]+=[c,c]}}
r.map(&:join)*$/}

0

Python, 319 bytes

def f(A):
 L,R,S=len,range,A.split('\n')
 if L(S[0])%2:S=[s+' 'for s in S]
 m=L(S[0])
 if L(S)%2:S+=[' '*m]
 C=[chr(int(sum(map(ord,[S[i][j],S[i+1][j],S[i][j+1],S[i+1][j+1]]))/4.0+0.5))for i in R(0,L(S),2)for j in R(0,m,2)]
 f=t='';i=0
 while i<L(C):
    t+=C[i]*2
    i+=1
    if i%(m/2)<1:f+=(t+'\n')*2;t=''
 f=f[:-1]
 print f

La segunda sangría es pestañas.

Lo anterior Ces el relleno, Ces el proceso de promediación de letras individuales y el resto se emite


0

R, 433 399 bytes

y=scan(,'',sep="\n")
h=nchar(y[1])
v=length(y)
p=function(x)paste(x,collapse="")
if(h%%2){y=sapply(y,function(x)paste0(x," "));h=h+1}
if(v%%2){y=c(y,p(rep(" ",h)));v=v+1}
z=matrix(unlist(lapply(y,function(x)strtoi(charToRaw(x),16))),ncol=h,byrow=T)
a=array(,c(v,h))
for(i in 1:(v/2)){for(j in 1:(h/2)){r=2*i-1:0;s=2*j-1:0;a[r,s]=rawToChar(as.raw(floor(mean(z[r,s])+.5)))}}
cat(apply(a,1,p),sep="\n")

Me estoy desesperando porque esta cosa parece no competir como diablos. Se imprime

$$>>II99######
$$>>II99######
$$##88KKGG####
$$##88KKGG####

para el caso de prueba.

Si te alimentas en el 7 × 3

%%%%%%%
Example
%%%%%%%

la salida será

BBFFJJ33
BBFFJJ33
######!!
######!!

debido a la divisibilidad por 2 etc. etc.

Sin golf:

y <- scan(, '', sep="\n") # Read STDIN and make it a character vector
h <- nchar(y[1]) # Get line width: how many chars per line
v <- length(y)   # Get array height: how many lines
p <- function(x) paste(x, collapse="") # A function that merges a vector of strings
if (h%%2) {y <- sapply(y, function(x) paste0(x, " ")); h=h+1} # If height is odd, add an empty line
if (v%%2) {y <- c(y, p(rep(" ", h))); v=v+1} # If width is odd, add an empty column
z <- matrix(unlist(lapply(y, function(x) strtoi(charToRaw(x), 16))), ncol=h, byrow=T)
# z now stores ASCII codes in a matrix; analogous to C strtol
a <- array(, dim=c(v,h)) # Reserve an array for the final result
for (i in 1:(v/2)) {
  for(j in 1:(h/2)) {
    r <- 2*i - 1:0 # Range of rows to average
    s <- 2*j - 1:0 # Range of columns to average
    a[r, s] <- rawToChar(as.raw(floor(mean(z[r, s]) + .5))) # Average, round, convert the ASCII codes
  } # and write them to the same place as in the original array
}
cat(apply(a, 1 , p), sep="\n") # Prints the array row-wise (index 1 for rows)

Solo mire cómo maneja este magnífico ejemplo (cortesía de chris.com):

                                                M$$$$$$$$$$$$$$$$RMMMMM8MMX    
                                               <$$$$$$$$$$$$R????!!?MMMR$RMMh. 
                                              :M$$$$$$$R?!!~~~~!!!!!!!MMM$$$$X 
                                             :M$$$$$$$X!~~~   ~~~~~!!MM8$MM$$M!
                                           :!XM$$$$$$R!~~~~~  ~~~~~!!M$$$$$$$R!
                                          <!XM$$$$$$MR!~~~~ ~~ ~:!!<:!M$$$$$$$!
                                         '<!XMBQQRMMMMX:::~~~<!?!!~~!!!!$$$M!$X
                                         ~!!MM$$$$$M8R!!!?!:~!!M$f?!~~~!M$8HXX?
                                       <!!!XMM$$$$$MMRM$$!~!~~~~~~~~~~~!XM?!!M!
                                      <!!!!XMM$$M$$MM$M!!~~~ ~~~ ~  :~!!!X!~~R!
                                      !!!!XMMM$MMMMMMMM!!~~~ ~~~~  ~~~!!!X~~X!~
                                      '~~!!MMMM@MMMX!!MM!:~!!!~~  `~~~~!!XXXX~ 
                                        ~!!!XMMMMMMMM!MMM!~~~~:: <<~~~~!!$$R!  
                                        '!!!!MM888M$MXMMM!!!!()!!~~~~~<!X$$$>  
                                         ~!!!M???M$MRRMM$X<~!!!!~~~~~:!XN$$M   
                                          ~!!!!!M$$$$@$@$$!!~~'  ~~:XH8$$$WR   
                                          !!!!MM$RM$RRMMMM?t!:::XX8$$$$$$$$>   
                                         ~~!!MMM$$$$$WX!!!!!!$$$$$$$RR$$$R"    
                                       '  <!!MT!!!~~~!#BX!!!~~?T#?!!!M$$$X.    
                                         <!!!!~~~~~~~~~?$!!!~~~~~~:!M$$$$MXH:  
                                         ~~~~~~~~~~~ ~~!M&!!!~~~<!!X$$$$$$$R$W>
                                        <~~~~~~~~    '~~!$!!!!!!!!MMRM$R?#!!$N!
                                       x~~~~~~~~~    ~~~!MX!!!!!!?!M!M!~~:!!$B!
                                      M!~~~~~~~~~~  <~~:!$R!!!!!!!X!!!~~!!~!RR!
                                    :M!~~~~~~~~~~~ `~~~!X$R!!!!!!!!~~~:!!~~tMM!
                                   dR!!~~~~~~~~~~~~~~~~!M$R!!!!!!!~!!!!!!~~@$@~
                                  tR!!!~~~~~~~~~~~~~~~!!M$R!!!!!!!!!!!!!~~!$$E~
                                 d!~~~~~~~~~~~~~~~~~~~!!$$X!!!!!!!!!!!!~~~X$$!~
                                8R~~~~~~~~~~~~~~~~~~~<!X$$!!!!!!!!!!!<~~~!MR$~~
                               8$~~~~~~~!~~~~~~~~~~~<!!$$R!!!MX!!!!~~~~~<XR$!~<
                             :$$!~~~~~~!!~~~~~~~~~~~!!M$$!!!!MM!!!~~~~~<!8$F~<!
                          .x8$$$!~~~~::!~~~~~~~~~~~!!X$$$!!!!MM!!~~~~~!!M$R~~~!
               .::xxxnHW8$$$$$$$$$$$$$$!!~~~~~~~~~~<!@$$X!!!!MM~~~~~<!!X$E~~~~!
          :t$$$$$$$$$$$$$$$$$$$$$$$$$$$!!~~~~~~~~~~!X$$$X~!!!MX~~~~~!!X$$~~~~!!
       ~~~~#R$$$$$RR8$$$$$$$$$$$$$$$$$$!!~~~~~~~~~<!M$$$B~!!!M!~~~~~!X$$!~~~~!!
     ~~~~~~~~?$$$$$$$$$$$$$$$$$$$$$$$$$!!~~~~~~~~~!!$$$$$X!!X$!~~~~!X$$R~~~~<!f
  :~~~~~~~~~`~?$$$$$$$$$$$$$$$$$$$$$$$$!!~~~~~~~~!!X$$$$$$X!M$~~~<!W$$R~~~~~!! 
 ~~~~~~~~~~  ~~M$$$$$$$$$$$$$$$$$$$$$$$!!~~~~~~~~!!M$$$$$$$$$Bid$$$$$$!~~~~~!! 
!~~~~~~~~~~~~~~~$$$$$$$$$$$$$$$$$$$$$$$!~~~~~~~~~!!$$$$$$$$$$$$$$$$$$!~~~~~~!~ 
!~~~~~~~~~~~~~~~M$$$$$$$$$$$$$$$$$$$$$$!:~~~~~~~<!X$$$$$$$$$$$$$$$$$!~~~~~~!!  
~~~~~~~~~~~~~~!!!$$$$$$$$$$$$$$$$$$$$$R!!:~~~~~~!!M$$$$$$$$$$$$$$"XR~~~~~~~!!  
~~~~~~~~~~~~~!!!!$$$$$$$$$$$$$$$$$$$$$R!<!~~~~~!!!X$$$$$$$$$$$P~  !~~~~~~~!!   
~~~~~~~~~~~<!!!!!$$$$$$$$$$$$$$$$$$$$$X!!~~~~~<!!!@#""`           ~~~~~~~~!!   
~~~~~~~~~~~!!!!!X$$$$$$$$$$$$$$$$$$$$$X!~~~~~~!!!                '~~~~~~~!!    
~~~~~~~~~~~<!!!!M$$$$$$$$$$$$$$$$$$$$$X!~~~~~!!!f                '~~~~~~<!!    
~~~~~~~~~~~~~!!!$$$$$$$$$$$$$$$$$$$$$R!~~~~~~!!!                 '~~~~~<!!>    
~~~~~~~~~~~~~~!!M$$$$$$$$$$$$$$$$$$$" !~~~~~!!!!                 ~~~~~~!!!     
~~~~~~~~~~~~~~!!!$$$$$$$$$$$$$$$$$R~ .!~~~~~!!!                  ~~~~~!!!!     
~~~~~~~~~~~~~~~!!!$$$$$$$$$$$$$*?!!!~!!~~~~<!!~                  ~~~~!!!!!     
~~~~~~~~~~~~~~~~!!!$$$$$$$$*"~!!!!!!!!!~~~~!!!                  <~~~:!!!!!     
!~~~~~~~~~~~~~~~~!!M$$$$#~~~~~~~~~~~!!~~~~~!!                   ~~~~~~~!!!     
!<~~~~~~~~~~~~~~~!!!R"~~~~~~~~~~~~~!!!~~~~<!!                  '~~~~~~<!!      
!<~~~~~~~~~~~~~~~~!!!~~~~~~~~~~~~~~!!~~~~~!!!X:                ~~~~~~~!!~      
!!~~~~~~~~~~~~~~~~!!!!~~~~~~~~~~~~<!!~~~~!!!9$MX:              ~~~~~~<!!       
!!!!!~~~~~~~~~~~~~!!!!:~~~~~~~~~~~!!~~~~~!!X$$$X!~            '~~~~~~!!~       
!!!!!!~~~~~~~~~~~<!!!!!!~~~~~~~~~~!!~~~~!!!M$$R!~~~~          ~~~~~~!!!        
!!!!!!!<~~~~~~~~~~~!!!!!!!:<:~~~~~!!~~~~!!X$$R!~~~~~~         ~~~~~~!!         
!!!!!!!!~~~~~~~~~~<!!!!!!!!!!!!!<!!!~~~~!!@$$!~~~~~~~~        ~~~~~!!~         
!!!!!!!!!~~~~~~~~:<!!!!X!!!!!!!!!!!!~~~!!X$$!~~~~~~~~~~~~:   '~~~~~!!          
'!!!!!!!!!!~~~~~~~!!!!!!?!!!!!!!!!!!~~<!!M$M!~~~~~~~~~~~~~~~ '~~~~~!~          
 !!!!!!!!!!\~~~~~~~>!!!!  `"MMMHX!!~~~<!!$R!~~~~~~~~~~~~~~~~~<~~~~!!           
  !!!!!!!!!!<~~~~~<!!!!!   .::<!!!!<~~!!X8X!~~~~~~~~~~~~~~~~~~~~~~!!           
  `X!!!!!!!!~~~~~!<!!!!!!!!!!!!~~!!\~~!!M$MX!:~~~~~~~~~~~~~~~~<!~~!>           
  !X!!!!!!!!~!!:!!!!!!!!!!~!!!:~~!!~~~!!$M!~~!!<~~~~~~~~~~~~~~~!!!!            
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:!!~~:!9R!~~~~~~!~~~~~~~~~~~~~~<!!f            
 'CHAT!!!!!!!!!!!!!!!!!!!!!!!!!!!!~~~!!MX~~~~~~~~~~~~~~~~~~~~~~!!X             

Después:

                                              ''..$$$$$$$$$$662266??MMIIDDEE66  
                                              ''..$$$$$$$$$$662266??MMIIDDEE66  
                                            ''>>$$$$$$<<WWOOOOggPP88,,HH99..;;  
                                            ''>>$$$$$$<<WWOOOOggPP88,,HH99..;;  
                                          ..HH..$$$$EEPP~~ggOOggVV??22..$$$$00!!
                                          ..HH..$$$$EEPP~~ggOOggVV??22..$$$$00!!
                                        99((PP77;;CCII5555VVVV4433WWPP88..<<==66
                                        99((PP77;;CCII5555VVVV4433WWPP88..<<==66
                                      ..!!==MM$$..99DD8899gggg~~ggOOmm88HH@@PP!!
                                      ..!!==MM$$..99DD8899gggg~~ggOOmm88HH@@PP!!
                                      ::88EEMM@@MMPP77BB??gg88~~88__~~88==kkTT88
                                      ::88EEMM@@MMPP77BB??gg88~~88__~~88==kkTT88
                                        ::!!EEHHCCCCEEMM77PPQQ00??nn~~??0000((  
                                        ::!!EEHHCCCCEEMM77PPQQ00??nn~~??0000((  
                                        8888,,00??..BB@@11??PP""ggmmVV55//GG    
                                        8888,,00??..BB@@11??PP""ggmmVV55//GG    
                                        8888,,MM00..HHEE77==((//>>));;$$00))    
                                        8888,,MM00..HHEE77==((//>>));;$$00))    
                                      ""''((,,\\PP~~ggII00!!~~ddXX''88$$;;<<''  
                                      ""''((,,\\PP~~ggII00!!~~ddXX''88$$;;<<''  
                                        VV~~~~~~ggOO99gg..!!88PP((EE::00++..;;((
                                        VV~~~~~~ggOO99gg..!!88PP((EE::00++..;;((
                                      BB~~~~~~~~gg  VVmm--;;!!!!))::,,gg??88CC!!
                                      BB~~~~~~~~gg  VVmm--;;!!!!))::,,gg??88CC!!
                                  11??PP~~~~~~~~~~__~~PP;;::!!!!!!PPPP''PPll@@88
                                  11??PP~~~~~~~~~~__~~PP;;::!!!!!!PPPP''PPll@@88
                                11YYPPgg~~~~~~~~~~~~~~!!..;;!!!!!!!!!!88~~00,,OO
                                11YYPPgg~~~~~~~~~~~~~~!!..;;!!!!!!!!!!88~~00,,OO
                              &&KK~~~~~~gg~~~~~~~~~~FF0000!!,,//!!88VV~~MMEEPP??
                              &&KK~~~~~~gg~~~~~~~~~~FF0000!!,,//!!88VV~~MMEEPP??
                          ::..$$PP~~mm??gg~~~~~~~~gg//..##!!777788~~~~??33OOnn!!
                          ::..$$PP~~mm??gg~~~~~~~~gg//..##!!777788~~~~??33OOnn!!
          <<""&&//NNLL::))$$$$$$$$$$$$##PP~~~~~~~~VV77$$TT!!77hh~~~~((==CC~~gg!!
          <<""&&//NNLL::))$$$$$$$$$$$$##PP~~~~~~~~VV77$$TT!!77hh~~~~((==CC~~gg!!
    88gg~~ggMM$$$$;;))$$$$$$$$$$$$$$$$##PP~~~~~~~~((..$$OO!!;;PP~~gg==00gg~~??22
    88gg~~ggMM$$$$;;))$$$$$$$$$$$$$$$$##PP~~~~~~~~((..$$OO!!;;PP~~gg==00gg~~??22
88mm~~~~~~gg__LL$$$$$$$$$$$$$$$$$$$$$$##PP~~~~~~PP::$$$$$$0066rrAA00$$\\~~~~!!  
88mm~~~~~~gg__LL$$$$$$$$$$$$$$$$$$$$$$##PP~~~~~~PP::$$$$$$0066rrAA00$$\\~~~~!!  
PP~~~~~~~~~~~~~~..$$$$$$$$$$$$$$$$$$$$##mm~~~~~~??00$$$$$$$$$$$$$$$$99~~~~gg88  
PP~~~~~~~~~~~~~~..$$$$$$$$$$$$$$$$$$$$##mm~~~~~~??00$$$$$$$$$$$$$$$$99~~~~gg88  
~~~~~~~~~~~~gg!!##$$$$$$$$$$$$$$$$$$$$::..~~~~gg!!;;$$$$$$$$$$FF""RR~~~~~~88    
~~~~~~~~~~~~gg!!##$$$$$$$$$$$$$$$$$$$$::..~~~~gg!!;;$$$$$$$$$$FF""RR~~~~~~88    
~~~~~~~~~~VV!!!!00$$$$$$$$$$$$$$$$$$$$==gg~~~~((!!))!!00        ""~~~~~~gg!!    
~~~~~~~~~~VV!!!!00$$$$$$$$$$$$$$$$$$$$==gg~~~~((!!))!!00        ""~~~~~~gg!!    
~~~~~~~~~~nn88!!..$$$$$$$$$$$$$$$$$$00FF~~~~PP!!22              $$~~~~nn((((    
~~~~~~~~~~nn88!!..$$$$$$$$$$$$$$$$$$00FF~~~~PP!!22              $$~~~~nn((((    
~~~~~~~~~~~~~~!!..$$$$$$$$$$$$$$$$FF$$PP~~~~!!!!                OO~~~~88!!      
~~~~~~~~~~~~~~!!..$$$$$$$$$$$$$$$$FF$$PP~~~~!!!!                OO~~~~88!!      
~~~~~~~~~~~~~~gg!!##$$$$$$&&::$$))!!88PP~~VV!!88                VV~~??!!!!      
~~~~~~~~~~~~~~gg!!##$$$$$$&&::$$))!!88PP~~VV!!88                VV~~??!!!!      
??~~~~~~~~~~~~~~PP,,//QQgg~~~~~~~~gg!!~~~~??!!                ""~~~~~~??!!      
??~~~~~~~~~~~~~~PP,,//QQgg~~~~~~~~gg!!~~~~??!!                ""~~~~~~??!!      
((~~~~~~~~~~~~~~~~!!88~~~~~~~~~~~~??PP~~gg!!66@@''            OO~~~~nn!!88      
((~~~~~~~~~~~~~~~~!!88~~~~~~~~~~~~??PP~~gg!!66@@''            OO~~~~nn!!88      
!!!!88~~~~~~~~~~nn!!!!??~~~~~~~~~~!!~~~~88::$$<<ggOO          hh~~~~8888        
!!!!88~~~~~~~~~~nn!!!!??~~~~~~~~~~!!~~~~88::$$<<ggOO          hh~~~~8888        
!!!!!!((~~~~~~~~~~??!!!!!!..??PPVV!!~~~~!!88//gg~~~~gg        ~~~~gg88          
!!!!!!((~~~~~~~~~~??!!!!!!..??PPVV!!~~~~!!88//gg~~~~gg        ~~~~gg88          
##!!!!!!88gg~~~~mm((!!//))!!!!!!!!!!~~??::..PP~~~~~~~~~~mmOO$$~~~~PP88          
##!!!!!!88gg~~~~mm((!!//))!!!!!!!!!!~~??::..PP~~~~~~~~~~mmOO$$~~~~PP88          
  !!!!!!!!00nn~~~~FF!!!!  44DD==//??~~((55;;~~~~~~~~~~~~~~~~nn~~~~!!            
  !!!!!!!!00nn~~~~FF!!!!  44DD==//??~~((55;;~~~~~~~~~~~~~~~~nn~~~~!!            
  LL!!!!!!!!ggVV88((!!!!!!88!!VVPPGG~~!!99QQ??VV~~~~~~~~~~~~~~??PP((            
  LL!!!!!!!!ggVV88((!!!!!!88!!VVPPGG~~!!99QQ??VV~~~~~~~~~~~~~~??PP((            
""3366!!!!!!!!!!!!!!!!!!!!!!!!!!''ggVV22RR~~~~~~gg~~~~~~~~~~~~VV//22            
""3366!!!!!!!!!!!!!!!!!!!!!!!!!!''ggVV22RR~~~~~~gg~~~~~~~~~~~~VV//22            

0

Ruby, 180 158 148 128 + 4 124 + 4 = 128 bytes

Ejecutar con $ ruby -nl(+4 bytes para -nlbanderas). Toma entrada en STDIN.

y,x=x,$_.scan(/..?/)
(puts [x.zip(y).map{|c|(("%2s"*2%c).bytes.reduce(:+)/4.0).round.chr*2}*""]*2
y,x=x,[])if$.%2<1||$<.eof?

Véalo en ideone: http://ideone.com/brmP3L

Sin golfos y explicación

Por ejemplo man ruby, la -nbandera "[c] hace que Ruby asuma el siguiente ciclo alrededor de su script ... while gets ... end". La variable especial $_contiene la última línea leída por gets. La -lbandera elimina el \nde cada línea, equivalente a $_.chop!.

y, x = x, $_.scan(/..?/)

( puts [
    x.zip(y).map {|c|
      (("%2s" * 2 % c).bytes.reduce(:+) / 4.0).round.chr * 2
    } * ""
  ] * 2
  y, x = x, []
) if $. % 2 < 1 || $<.eof?

La variable especial $.es el número de líneas que se han leído hasta ahora, y $<es STDIN. Los pares de caracteres de cada segunda línea se comprimen con los de la línea anterior. La cadena de formato %2s%2scombina los caracteres y los rellena con espacios, luego los caracteres se promedian.


1
w=s=~/$/es más corto para calcular el ancho inicial. También solo devuelva la cadena calculada en lugar de imprimir porque eso cuenta como salida
Value Ink
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.