Bit manipulation

Given two list of bit patterns and two bit strings, extract bits from the first string with the first pattern and place them in the second string with the second pattern. Draw a diagram above each value with numbers indicating the order of extraction and insertion (see tests for details). It should also work for text, not just binary digits. (see test 7)

IN

From: 010011010
Bits: 1:0, 6:4, 8
To:   000000000
Bits: 7:2

OUT

From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
010001000

IN

From: 0000011111
Bits: 9, 0, 8, 1, 7, 2, 6, 3, 5:4
To:   11111111110000000000
Bits: 14:5

OUT

From:
13579 8642
^^^^/\^^^^
0000011111

=0101010101

To:
     1
     /--------\
11111010101010100000

IN

From: 010011010
Bits: 6:4, 1:0, 8
To:   000000000
Bits: 7:2

OUT

From:
3 1    2
^ /-\  /\
010011010

=001100

To:
 1
 /----\
000110000

IN

From: 100100010000100000
Bits: 5:0, 10:6, 14:11, 17:15
To:   000000000000000000
Bits: 17:15, 14:11, 10:6, 5:0

OUT

From:
4  3   2    1
/-\/--\/---\/----\
100100010000100000

=100000100001000100

To:
1  2   3    4
/-\/--\/---\/----\
100000100001000100

IN

From: 100100010000100000
Bits: 5:0, 10:6, 14:11, 17:15
To:   000000000000000000
Bits: 17:12, 11:7, 6:3, 2:0

OUT

From:
4  3   2    1
/-\/--\/---\/----\
100100010000100000

=100000100001000100

To:
1     2    3   4
/----\/---\/--\/-\
100000100001000100

IN

From: 0011010010101100100101
Bits: 0, 2, 5, 9:8, 11, 13, 16, 19:18
To:   000000000000
Bits: 11, 8:0

OUT

From:
  8  7  6 5 4   3  2 1
  /\ ^  ^ ^ /\  ^  ^ ^
0011010010101100100101

=1111111111

To:
1  2
^  /-------\
100111111111

IN

From: saint
Bits: 4, 0, 3:1
To:   _____
Bits: 4, 2, 3, 1:0

OUT

From:
13  2
^/-\^
saint

=stain

To:
1324
^^^/\
satin
Login to submit solution


464 symbols perl failed test 1 Wrong answer
From:
3 2    1 
^ /-\  /\
010011010

=100010

To:
 1       
 /----\  
010001000
instead of
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
010001000
Thu Apr 21 01:46:33 2016 megahallon
#!/usr/bin/perl
@i=map{chomp;substr $_,6}<>;sub l{length $_[0]}sub z{$p=defined $3?$3:$1;$l=$1-$p+1;$p+=$l;substr $f[$_[0]],-$p,$l,++$n x$l}@f=map{' 'x l $_}@i[0,2];$m=qr/(\d+)(:(\d+))?/;while($i[1]=~/$m/g){z(0);$r.=substr $i[0],-$p,$l}$n=0;while($i[3]=~/$m/g){z(1);substr $i[2],-$p,$l,substr $r,$q,$l;$q+=$l}@p=map{s/(\d)\1*/$1.' 'x(l($&)-1)/ger}@f;@g=map{s#(\d)\1*#l($&)!=1?"/"."-"x(l($&)-2)."\\":"^"#ger}@f;print"From:\n$p[0]\n$g[0]\n$i[0]\n\n=$r\n\nTo:\n$p[1]\n$g[1]\n$i[2]";
413 symbols perl failed test 4 Wrong answer
From:
4  3   2    1
/-\/--\/---\/----\
100100010000100000

=100000100001000100

To:
1  2   3    4
/-\/--\/---\/----\
100
instead of
From:
4  3   2    1
/-\/--\/---\/----\
100100010000100000

=100000100001000100

To:
1  2   3    4
/-\/--\/---\/----\
100000100001000100
Tue May 3 02:39:52 2016 megahallon
#!/usr/bin/perl
@i=map{chomp;substr$_,6}<>;sub s{length$&};sub z{$n=0;while($i[$w*2+1]=~/(\d+)(:(\d+))?/g){$p=-$1-1;$l=-($3//$1)-$p;substr$f[$w],$p,$l,++$n x$l;$w?(substr($i[2],$p,$l,substr$r,$q,$l),$q+=$l):$r.=substr$i[0],$p,$l}$w++}@f=map{' 'x length$_}@i[0,2];z;z;($p,$w)=map{s/(\d)\1*/$1.' 'x(&s-1)/ger}@f;($g,$h)=map{s#(\d)\1*#&s-1?'/'.'-'x(&s-2).'\\':'^'#ger}@f;$_="From:!$p!$g!$i[0]!!=$r!!To:!$w!$h!$i[2]";s/ *!/\n/g;print
459 symbols bash failed test 1 Wrong answer
From:
3 2    1 
^ /-\  /\
010011010

=100010

To:
 1       
 /----\  
010001000
instead of
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
010001000
Fri Aug 12 22:57:55 2016 hidefromkgb
g(){ rev<<<"$1"|fold -w1;}
h(){ p=${@: -1};k=(`yes _|head -${#p}`);p=(${k[@]});l=(`g $3`);r=(`g $2`)
for x in ${1//,/ };do n=${x#*:};x=m=${x%:*};k[$x]=$[++j]
while((m>=n));do p[$m]=-;l[$[${#3}?m--:y++]]=${r[$[${#3}?y++:m--]]};done
p[$x]=\/;p[$n]=^;((n<x))&&p[$n]=\\
done;rev<<<${k[@]}$'\n'${p[@]}$'\n'${l[@]}|sed 's/ //g;y/_/ /';}
read -d $'\4' s i d o<<<`tr -d \ |cut -d: -f2-`
i=`h $i $s`;e=${i##*$'\n'};echo "From:
${i%$e}$s

=`rev<<<$e`

To:
`h $o $e $d`"
462 symbols bash failed test 7 Wrong answer
From:
13  2
^/-\^
saint

=0001

To:
1324
^^^/\
00010
instead of
From:
13  2
^/-\^
saint

=stain

To:
1324
^^^/\
satin
Fri Aug 12 23:20:48 2016 hidefromkgb
g(){ rev<<<"$1"|fold -w1;}
h(){ p=${@: -1};k=(`yes _|head -${#p}`);p=(${k[@]});l=(`g $3`);r=(`g $2`)
for x in ${1//,/ };do n=${x#*:};x=m=${x%:*};k[$x]=$[++j]
while((m>=n));do p[$m]=-;((l[${#3}?m--:y++]=r[${#3}?y++:m--]));done
p[$x]=\/;p[$n]=^;((n<x))&&p[$n]=\\
done;rev<<<${k[@]}$'\n'${p[@]}$'\n'${l[@]}|sed 's/ //g;y/_/ /;s/ *$//';}
read -d $'\4' s i d o<<<`tr -d \ |cut -d: -f2-`
i=`h $i $s`;e=${i##*$'\n'};echo "From:
${i%$e}$s

=`rev<<<$e`

To:
`h $o $e $d`"
456 symbols bash failed test 1 Wrong answer
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\  
010001000
instead of
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
010001000
Sat Aug 13 00:51:35 2016 hidefromkgb
g()(rev<<<"$1"|fold -w1)
h()(p=${@: -1};k=(`yes _|head -${#p}`);p=(${k[@]});l=(`g $3`);r=(`g $2`)
for x in ${1//,/ };do n=${x#*:};x=m=${x%:*};k[$x]=$[++j]
while((m>=n));do p[$m]=-;l[$[${#3}?m--:y++]]=${r[$[${#3}?y++:m--]]};done
p[$x]=\/;p[$n]=^;((n<x))&&p[$n]=\\
done;sed "s/ //g;y/_+/ \n/;s/^ *//"<<<${k[@]}+${p[@]}+${l[@]}|rev)
read -d $'\4' s i d o<<<`tr -d \ |cut -d: -f2-`
i=`h $i $s`;e=${i##*$'\n'};echo "From:
${i%$e}$s

=`rev<<<$e`

To:"
h $o $e $d
159 symbols pyth failed test 1 Wrong answer
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
instead of
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
010001000
Mon Oct 31 13:04:53 2016 Sait2000
J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)Lsm|hxhCbd;hheSbMu:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^HG"From:"jbgByGGhJkp\=Tk"To:"jbgByHH
204 symbols pyth failed test 2 Wrong answer
From:
25 8642
/\^^^^^^^6^^
0000011111

=0101010101

To:
     1
     /--------\
11111010101010100000
instead of
From:
13579 8642
^^^^/\^^^^
0000011111

=0101010101

To:
     1
     /--------\
11111010101010100000
Mon Oct 31 13:28:00 2016 Sait2000
J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)Lsm|hxhCbd;hheSbMu:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^HG"From:"jbgByGGhJkp\=Tk"To:"jbgByHHhu[j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeH)H,@J2T
215 symbols pyth failed test 1 Wrong answer
From:
121110916151413
^\^-\^\^\^\09^\6^\5^\4^\3
010011010

=100010

To:
1211
1/----\11
010001000
instead of
From:
3 2    1
^ /-\  /\
010011010

=100010

To:
 1
 /----\
010001000
Mon Oct 31 13:55:34 2016 Sait2000
J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)Mu:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^HG"From:"jbgBjkm|hxhCbd;hheSGGhJkp\=Tk"To:"jbgBjkm|hxhCbd;hheSHHhu,j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeHH,@J2T
133 symbols pyth failed test 1 Code error

Traceback (most recent call last): File "/usr/bin/pyth.py", line 713, in <module> exec(py_code_line, environment) File "<string>", line 1, in <module> File "/usr/bin/pyth.py", line 458, in pyth_eval return eval(parse(a)[0], environment) File "<string>", line 1, in <module> File "/usr/bin/macros.py", line 182, in assign raise BadTypeCombinationError("=", a, b) macros.BadTypeCombinationError: Error occured in function: = Arg 1: '[G,H]', type str. Arg 2: [], type list.

Mon Oct 31 14:17:08 2016 Sait2000
 .v.ZCim-i%Vd[16K64K)KJ6479cCM"ꈅ少鹊櫙粻┽絬䬦籓糅멊擊⃧鐙헉깍蘭퓄륄㵑䜽吂첋樧겎豀땘愷䭒摤쵰㱘랺扨绅踽㪆愩컽♫朗ᫎ噿堂㴃꣥附搊触竘漷怘译䂏ﱽ춗潦옆불␪术匕ꢟ귿䎳㨨숡桋궪⮣䲭竰肺ⳬ珄ꦛⳅ馪늟쳜懄⼞䱈⛦지츤"3*J9
136 symbols pyth failed test 1 Code error

Traceback (most recent call last): File "/usr/bin/pyth.py", line 713, in <module> exec(py_code_line, environment) File "<string>", line 1, in <module> TypeError: times() missing 1 required positional argument: 'b'

Mon Oct 31 14:19:00 2016 Sait2000
 .v.ZCim-i%Vd[16=Y64Y)=YJ6479cCM"ꈅ少鹊櫙粻┽絬䬦籓糅멊擊⃧鐙헉깍蘭퓄륄㵑䜽吂첋樧겎豀땘愷䭒摤쵰㱘랺扨绅踽㪆愩컽♫朗ᫎ噿堂㴃꣥附搊触竘漷怘译䂏ﱽ춗潦옆불␪术匕ꢟ귿䎳㨨숡桋궪⮣䲭竰肺ⳬ珄ꦛⳅ馪늟쳜懄⼞䱈⛦지츤"3*=Y9
135 symbols pyth failed test 1 Code error

Traceback (most recent call last): File "/usr/bin/pyth.py", line 713, in <module> exec(py_code_line, environment) File "<string>", line 1, in <module> TypeError: base_10() missing 1 required positional argument: 'b'

Mon Oct 31 14:19:39 2016 Sait2000
 .v.ZCim-i%Vd[16=Y64Y)=YJ6479cCM"ꈅ少鹊櫙粻┽絬䬦籓糅멊擊⃧鐙헉깍蘭퓄륄㵑䜽吂첋樧겎豀땘愷䭒摤쵰㱘랺扨绅踽㪆愩컽♫朗ᫎ噿堂㴃꣥附搊触竘漷怘译䂏ﱽ춗潦옆불␪术匕ꢟ귿䎳㨨숡桋궪⮣䲭竰肺ⳬ珄ꦛⳅ馪늟쳜懄⼞䱈⛦지츤"3*Y9
135 symbols pyth failed test 1 Code error

Traceback (most recent call last): File "/usr/bin/pyth.py", line 713, in <module> exec(py_code_line, environment) File "<string>", line 1, in <module> File "/usr/bin/pyth.py", line 458, in pyth_eval return eval(parse(a)[0], environment) File "<string>", line 1 Plist(assign("J",Pmap(lambda d:gt(d,6),all_input())),assign('T',Psum(binary_map(lambda N, Y:lt(gt(head(J),N),Y),head(assign('[G,H]',Pmap(lambda d:Pmap(lambda k:[minus(tail(Plen(head(d))),assign("K",Punsafe_eval(lookup(k,1)))),Pexcept(lambda:head(minus(K,Punsafe_eval(end(k)))), lambda:1)],at_slice(end(d),"(\d+)(:(\d+))?",4)),chop(J,2))))))),@memoized ^ SyntaxError: invalid syntax

Mon Oct 31 14:20:45 2016 Sait2000
 .v.ZCim-i%Vd[16=Y64Y)Y=Y6479cCM"ꈅ少鹊櫙粻┽絬䬦籓糅멊擊⃧鐙헉깍蘭퓄륄㵑䜽吂첋樧겎豀땘愷䭒摤쵰㱘랺扨绅踽㪆愩컽♫朗ᫎ噿堂㴃꣥附搊触竘漷怘译䂏ﱽ춗潦옆불␪术匕ꢟ귿䎳㨨숡桋궪⮣䲭竰肺ⳬ珄ꦛⳅ馪늟쳜懄⼞䱈⛦지츤"3*Y9

View all solutions