Tag Info

2048-like array shift

Jelly, 10 9 8 bytes Œg+2/€UF TryItOnline or run all test cases How? ...
• 106k

2048-like array shift

Haskell, 47 57 50 bytes e#l|a:b<-l,e==a= -2*a:b|1<2=e:l map abs.foldr(#)[] Uses reduce (or ...
• 35.8k
Accepted

• 13.3k

2048-like array shift

GNU sed, 41 38 37 Includes +1 for -r -3 Thanks to Digital Trauma -1 Thanks to seshoumara : s,(.*)(1+) \2\b,\1!\2\2!, t y,!, , Input and output are space ...
• 11.7k

2048-like array shift

Python, 61 bytes def f(l):b=l[-2:-1]==l[-1:];return l and f(l[:~b])+[l[-1]<<b] The Boolean b checks whether the last ...
• 145k

Is it a good 2048 board?

APL (Dyalog), 7 4 bytes Takes 4-by-4 matrix, using 0 for blanks, as argument. ⊢≡⌊⍀ Try it online! ⌊⍀ is the vertical ...
• 30.2k

2048-like array shift

JavaScript (ES6), 68 bytes ...
• 81

2048-like array shift

Perl, 41 bytes Includes +1 for -p Give input sequence on STDIN: shift2048.pl <<< "2 2 2 4 4 8 2" ...
• 14.9k
Accepted

Solve the 15 Puzzle (the tile-sliding puzzle)

PyPy, 195 moves, ~12 seconds computation Computes optimal solutions using IDA* with a 'walking distance' heuristic augmented with linear conflicts. Here are the optimal solutions: ...
• 39.1k

2048-like array shift

Retina, 32 \d+ $* r`\b\1 (1+)\b$1$1 1+$.& r on line 3 activates right-to-left regex matching. And this means that the <...
• 72.1k

2048-like array shift

Perl, 43 + 1 (-p) = 44 bytes Ton Hospel came up with 41 bytes answer, check it out! -4 thanks to @Ton Hospel! Edit : added ...
• 8,473

2048-like array shift

Perl 5.10, 61 50 bytes (49 + 1 for flag) Thanks to Ton Hospel for saving 11 bytes! Regex-free solution, with -a flag: ...
• 901

2048-like array shift

JavaScript (ES6),  68...64  58 bytes a=>(g=p=>(x=a.pop())|p?p?[...g(p^x?x:!(p*=2)),p]:g(x):a)() Try it online! Commented ...
• 192k

Is it a good 2048 board?

Jelly, 4 bytes Ṣ€U⁼ Try it online! Input as an array of columns. Works with arbitrary-sized grids.
• 42.4k

Is it a good 2048 board?

R (+pryr), 23 bytes pryr::f(all(diff(x))<1) Which evaluates to the function function (x) all(diff(x)) < 1 Which ...
• 2,984

2048-like array shift

05AB1E, 26 bytes D¥__X¸«DgL*ê¥X¸«£vy2ôO})í˜ Try it online! Generalized steps Reduce by subtraction to find where consecutive elements differ Reduce by ...
• 53k

Is it a good 2048 board?

Python 3, 42 bytes lambda l:all(x==sorted(x)[::-1]for x in l) Try it online! Same algorithm as my Jelly answer
• 42.4k

2048-like array shift

Java 7, 133 bytes Object f(java.util.ArrayList<Long>a){for(int i=a.size();i-->1;)if(a.get(i)==a.get(i-1)){a.remove(i--);a.set(i,a.get(i)*2);}return a;} ...
• 19.7k

2048-like array shift

Haskell, 56 bytes g(a:b:r)|a==b=a+b:g r|l<-b:r=a:g l g x=x r=reverse r.g.r
• 2,447

2048-like array shift

Mathematica, 53 bytes Join@@(Reverse[Plus@@@#~Partition~UpTo@2]&/@Split@#)& Explanation Split@# Split the input ...
• 13.8k

Is it a good 2048 board?

JavaScript, 37 bytes x=>''+x==x.map(v=>v.sort((x,y)=>y-x)) Call it like this: ...
• 34.7k

2048-like array shift

Perl, 37 bytes Includes +4 for -0n Run with the input as separate lines on STDIN: perl -M5.010 shift2048.pl 2 2 2 4 4 8 2 ^D ...
• 14.9k

2048-like array shift

PHP, 86 100 99 94 bytes for($r=[];$v=+($p=array_pop)($a=&$argv);)array_unshift($r,end($a)-$v?$v:2*$p($a));print_r($r); requires PHP 7.0; takes values from ...
• 14.7k