Skip to main content
added 912 characters in body
Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 34 bytes

⊞υ⮌SFISBH42≔ΣEυ…⊞OE⪪κι∨μIν0²υI⍘⮌υ² 

Attempt This Online!Attempt This Online! Link is to verbose version of code. Will produce values up to 18446744073709551615. Explanation:

⊞υ⮌S 

Start with the reverse of the input in a single piece.

FISBH42 

Work down from the highest double power of two to the lowest.

≔ΣEυ…⊞OE⪪κι∨μIν0²υ 

Split each piece into two, defaulting the first piece to 0 and the second piece to 1, but if the split only resulted in one piece then pad it with a 0 piece. Examples: 2 splits on 2 to give two empty strings, so the first becomes 0 and the second 1, representing 10 in binary; 21 splits on 2 to give 1 and an empty string, the latter becoming 1, representing 11 in binary; 1 splits on 2 to give 1, which is padded with a 0, representing 01 in binary.

I⍘⮌υ² 

The reversed list is now a list of binary digits so convert it to decimal.

A port of @Pufe's Ruby answer is also 34 bytes:

≔⁰θFESX²÷X²⌕124HBSIι²≧⁺∨×﹪θι⊖ιιθIθ 

Try it online! Link is to verbose version of code. Explanation:

≔⁰θ 

Start with zero.

FESX²÷X²⌕124HBSIι² 

Map the characters of the input string to the double powers of 2 and loop over the results.

≧⁺∨×﹪θι⊖ιιθ 

Add on the current value modulo the current power multiplied by one less than the current power, unless that is 0, in which case just add on the current value.

Iθ 

Output the final result.

Charcoal, 34 bytes

⊞υ⮌SFISBH42≔ΣEυ…⊞OE⪪κι∨μIν0²υI⍘⮌υ² 

Attempt This Online! Link is to verbose version of code. Will produce values up to 18446744073709551615. Explanation:

⊞υ⮌S 

Start with the reverse of the input in a single piece.

FISBH42 

Work down from the highest double power of two to the lowest.

≔ΣEυ…⊞OE⪪κι∨μIν0²υ 

Split each piece into two, defaulting the first piece to 0 and the second piece to 1, but if the split only resulted in one piece then pad it with a 0 piece. Examples: 2 splits on 2 to give two empty strings, so the first becomes 0 and the second 1, representing 10 in binary; 21 splits on 2 to give 1 and an empty string, the latter becoming 1, representing 11 in binary; 1 splits on 2 to give 1, which is padded with a 0, representing 01 in binary.

I⍘⮌υ² 

The reversed list is now a list of binary digits so convert it to decimal.

Charcoal, 34 bytes

⊞υ⮌SFISBH42≔ΣEυ…⊞OE⪪κι∨μIν0²υI⍘⮌υ² 

Attempt This Online! Link is to verbose version of code. Will produce values up to 18446744073709551615. Explanation:

⊞υ⮌S 

Start with the reverse of the input in a single piece.

FISBH42 

Work down from the highest double power of two to the lowest.

≔ΣEυ…⊞OE⪪κι∨μIν0²υ 

Split each piece into two, defaulting the first piece to 0 and the second piece to 1, but if the split only resulted in one piece then pad it with a 0 piece. Examples: 2 splits on 2 to give two empty strings, so the first becomes 0 and the second 1, representing 10 in binary; 21 splits on 2 to give 1 and an empty string, the latter becoming 1, representing 11 in binary; 1 splits on 2 to give 1, which is padded with a 0, representing 01 in binary.

I⍘⮌υ² 

The reversed list is now a list of binary digits so convert it to decimal.

A port of @Pufe's Ruby answer is also 34 bytes:

≔⁰θFESX²÷X²⌕124HBSIι²≧⁺∨×﹪θι⊖ιιθIθ 

Try it online! Link is to verbose version of code. Explanation:

≔⁰θ 

Start with zero.

FESX²÷X²⌕124HBSIι² 

Map the characters of the input string to the double powers of 2 and loop over the results.

≧⁺∨×﹪θι⊖ιιθ 

Add on the current value modulo the current power multiplied by one less than the current power, unless that is 0, in which case just add on the current value.

Iθ 

Output the final result.

Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 34 bytes

⊞υ⮌SFISBH42≔ΣEυ…⊞OE⪪κι∨μIν0²υI⍘⮌υ² 

Attempt This Online! Link is to verbose version of code. Will produce values up to 18446744073709551615. Explanation:

⊞υ⮌S 

Start with the reverse of the input in a single piece.

FISBH42 

Work down from the highest double power of two to the lowest.

≔ΣEυ…⊞OE⪪κι∨μIν0²υ 

Split each piece into two, defaulting the first piece to 0 and the second piece to 1, but if the split only resulted in one piece then pad it with a 0 piece. Examples: 2 splits on 2 to give two empty strings, so the first becomes 0 and the second 1, representing 10 in binary; 21 splits on 2 to give 1 and an empty string, the latter becoming 1, representing 11 in binary; 1 splits on 2 to give 1, which is padded with a 0, representing 01 in binary.

I⍘⮌υ² 

The reversed list is now a list of binary digits so convert it to decimal.