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


126 symbols pyth
Thu Nov 3 11:27:54 2016 Sait2000
.v.ZCim-id64^964 2cCM"ა꾁璏肈㿂ꅦ⭫練⩂孲㵦쪱๱脄㧓埁툙芄뙷췷ᡊ筄걂娿㝄饿醌ꎐᮁᯟᇄ䨯䎨仦䤤民陗꟏즷蹉䭋ﻨ굦砽蒀耼墼촻蜮㑕놔愑촷㖲쫴▆㪎ꪒᏭ끏浲茠笎챸틼椇췃猺鉝㯃첎俙㪳賋觱쒸ꌓ퐶ᓓ༭欻爾갖泝⛟饘"3 64218
135 symbols pyth
Mon Oct 31 14:32:15 2016 Sait2000
 .v.ZCim-i%Vd[16=Y64Y)Y=Y6485cCM"᧒ⵗ숈⧍㾶汫쿷ᷗ籑㉝떻碉䇗啼潰췸㌩歂펻爿뺿恰㌛찠훑睖⎘薈皲ᴠ‿쨹▒壵楙峌濔⦃粺⼊䓈啃ꂷ㯱⚍큸⺙蠔餤ⳣ⳯⑏졎怦鄨㊾ꋌỐ䆦㬣㖅껍认䍽콰ⳙꅛ窖珪ꞁꏓ䴱ꊮᰩ닻쀶깗돕삻䊧⮂꘾鲂茵ꛋ짡㕝"3*Y9
200 symbols pyth
Mon Oct 31 14:02:48 2016 Sait2000
J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)Ljkm|hxhCbd;hheSbMu:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^HG"From:"g
yGGhJkp\=Tk"To:"g
yHHhu,j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeHH,@J2T
204 symbols pyth
Mon Oct 31 13:42:50 2016 Sait2000
J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)Ljkm|hxhCbd;hheSbMu:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^HG"From:"jbgByGGhJkp\=Tk"To:"jbgByHHhu,j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeHH,@J2T
205 symbols pyth
Mon Oct 31 13:34:08 2016 Sait2000
J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)Ljkm|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
Mon Oct 31 13:57:10 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|hxhCGd;hheSGGhJkp\=Tk"To:"jbgBjkm|hxhCHd;hheSHHhu,j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeHH,@J2T
258 symbols pyth
Mon Oct 31 14:25:23 2016 Sait2000
 [J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed"(\d+)(:(\d+))?"4cJ2)
"From:"
u:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^G
jkm|hxhCGd;hheSG
hJ
kp\=
T
k
"To:"
u:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^H
jkm|hxhCHd;hheSH
hu,j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeHH,@J2T
291 symbols pyth
Mon Oct 31 14:04:33 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:"u:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^G
jkm|hxhCGd;hheSGhJkp\=Tk"To:"u:G+@GhHX1"()?"*dteH?teH++\/*tteH\-*2\\\^H
jkm|hxhCHd;hheSHhu,j<eGeHt:hGX2X3"(^)"*eH\.*hH\.3>eGeHH,@J2T
300 symbols pyth
Mon Oct 31 14:29:51 2016 Sait2000
 .v"[J>R6.z=Ts.b<>hJNYhAmm,-tlhdKv@k1.xh-Kvek1:ed\"(\\d+)(:(\\d+))?\"4cJ2)\n\"From:\"\nu:G+@GhHX1\"()?\"*dteH?teH++\\/*tteH\\-*2\\\\\\^G\njkm|hxhCGd;hheSG\nhJ\nkp\\=\nT\nk\n\"To:\"\nu:G+@GhHX1\"()?\"*dteH?teH++\\/*tteH\\-*2\\\\\\^H\njkm|hxhCHd;hheSH\nhu,j<eGeHt:hGX2X3\"(^)\"*eH\\.*hH\\.3>eGeHH,@J2T"
414 symbols perl
Tue May 3 03:06:10 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
445 symbols bash
Sat Aug 13 06:20:00 2016 hidefromkgb
g()(rev<<<$@|fold -w1)
h()(p=${3:-$2};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;g ${l[@]}+${p[@]}+${k[@]}+:$4|tr -d \\n\ )
read -d $'\4' s i d o<<<`cut -d: -f2-|tr -d \ `;i=`h $i $s`;e=${i##*+}
h $o $e $d oT++$e=++`rev<<<${i%$e}$s`morF|sed s/_*+/\\n/g|tr _ \ 
446 symbols bash
Sat Aug 13 05:49:30 2016 hidefromkgb
g()(rev<<<$1|fold -w1)
h()(p=${3:-$2};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;tr -d \ <<<${l[@]}+${p[@]}+${k[@]}+:$4|rev)
read -d $'\4' s i d o<<<`tr -d \ |cut -d: -f2-`;i=`h $i $s`;e=${i##*+}
h $o $e $d oT++$e=++`rev<<<${i%$e}$s`morF|sed s/_*+/\\n/g|tr _ \ 
449 symbols bash
Sat Aug 13 04:47:13 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;tr -d \ <<<${l[@]}+${p[@]}+${k[@]}+:|rev)
read -d $'\4' s i d o<<<`tr -d \ |cut -d: -f2-`;i=`h $i $s`;e=${i##*+}
sed 's/_*+/\n/g;y/_/ /'<<<From${i%$e}$s`rev<<<oT++$e=++``h $o $e $d`
456 symbols bash
Sat Aug 13 04:04:41 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;s/+_*/\n/g;y/_/ /'<<<:+${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`"
461 symbols bash
Sat Aug 13 00:01:32 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
463 symbols bash
Fri Aug 12 23:51:24 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`"
465 symbols perl
Thu Apr 21 12:08:16 2016 megahallon
#!/usr/bin/perl
@i=map{chomp;substr$_,6}<>;sub l{length$_[0]}sub z{$p=$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,$w)=map{s/(\d)\1*/$1.' 'x(l($&)-1)/ger=~s/\s+$//r}@f;($g,$h)=map{s#(\d)\1*#l($&)-1?'/'.'-'x(l($&)-2).'\\':'^'#ger=~s/\s+$//r}@f;print"From:\n$p\n$g\n$i[0]\n\n=$r\n\nTo:\n$w\n$h\n$i[2]";
467 symbols bash
Fri Aug 12 22:54:07 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`"
468 symbols perl
Thu Apr 21 11:55:41 2016 megahallon
#!/usr/bin/perl
@i=map{chomp;substr$_,6}<>;sub l{length$_[0]}sub z{$p=$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,$w)=map{s/(\d)\1*/$1.' 'x(l($&)-1)/ger=~s/\s+$//r}@f;($g,$h)=map{s#(\d)\1*#l($&)!=1 ? "/"."-"x(l($&)-2)."\\":"^"#ger=~s/\s+$//r}@f;print"From:\n$p\n$g\n$i[0]\n\n=$r\n\nTo:\n$w\n$h\n$i[2]";
470 symbols bash
Fri Aug 12 22:49: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=${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`"

View all solutions