7
\$\begingroup\$

(spoilers on decade-old TV show below)

In the HBO show "The Wire", the dealers use a system of encoding phone numbers where they "jump the 5" on the number pad.

+-----------+
| 1 | 2 | 3 |
+-----------+
| 4 | 5 | 6 |
+-----------+
| 7 | 8 | 9 |
+-----------+
| # | 0 | * |
+-----------+

A "1" becomes a "9", "4" becomes "6", etc ... and "5" and "0" swap places. So, the input number 2983794-07 becomes 8127316-53. # and * are undefined. Input in the show was numeric with a dash, your code does not need to preserve the dash but it does need to accept it. No real restrictions on how user interaction is handled, can be a function or console application as needed.

Scoring criteria:

This is a code golf, fewest characters wins. There is a 100 character penalty for solving it with a number/character swap or array lookup (I'm trying to broadly paint a brush stroke to avoid "9876043215"[i]-style lookups). 10 character bonus for not stripping dashes.

\$\endgroup\$
7
  • 3
    \$\begingroup\$ Isn't every solution going to use number/character swap with or without a detour? \$\endgroup\$ Oct 21, 2014 at 15:04
  • \$\begingroup\$ Maybe I should make it a pop-con question instead, to get creative answers out of it? \$\endgroup\$ Oct 21, 2014 at 15:05
  • 8
    \$\begingroup\$ Isn't this just ten minus each digit? (Beside 5/0) \$\endgroup\$
    – Geobits
    Oct 21, 2014 at 15:05
  • 5
    \$\begingroup\$ @insta Taking a simple task and saying "solve this creatively", generally makes for a very bad popularity contest. \$\endgroup\$ Oct 21, 2014 at 15:06
  • 9
    \$\begingroup\$ "There is a 100 character penalty for solving it with a number/character swap or array lookup." This is a vague and unenforceable rule. If your challenge has a boring optimal solution, you should rethink the challenge rather than try to ban that solution. \$\endgroup\$
    – xnor
    Oct 21, 2014 at 21:28

6 Answers 6

10
\$\begingroup\$

MATLAB, 25 bytes

Function accepting a string.

- is converted to =... so the dash is not just preserved, there is twice as much of it?

@(n)106-n-5*(n==48|n==53)
\$\endgroup\$
3
  • 14
    \$\begingroup\$ LOL twice as much :D \$\endgroup\$
    – Optimizer
    Oct 21, 2014 at 15:24
  • \$\begingroup\$ I think you need to add a few bytes. You need to convert the answer from int to char. I think the easiest way is: f=@(n)['' 106-n-5*(n==48|n==53)]. (Fewer bytes than char(...).) \$\endgroup\$ Oct 22, 2014 at 11:27
  • \$\begingroup\$ @RobertP. True, it will display numbers if you simply evaluate f on the command line, but it will work fine if used where a string is expected, e.g. fprintf('%s\n', f('3259232352')) \$\endgroup\$
    – feersum
    Oct 22, 2014 at 14:39
9
\$\begingroup\$

tr, 107 (17 + 100 - 10)

tr 0-9 5987604321
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Perl port: y/0-9/5987604321/ and -p flag. Run with: perl -pe'y/0-9/5987604321/' <(echo 2983794-07) Score is 17 + 1 + 100 - 10 = 108. \$\endgroup\$
    – hmatt1
    Oct 22, 2014 at 22:36
8
\$\begingroup\$

GolfScript 9 (= 19 - 10)

{16|106\-.5%3=5*-}%

or

{16|~107+.5%3=5*-}%

Online demo

Explanation

As observed by a few people, this calls for subtraction of the digit from 10 in most cases. Since the digits in ASCII start at 48, that means subtracting the ASCII code from 48*2+10 = 106. That leaves three special cases:

  • 0 and 5: these map to 10 and 5 respectively, and need to map to 5 and 0. Solution: .5%3=5*- subtracts 5 if the result is equal to 3 modulo 5.
  • -: this is the fun one. ASCII code 45, so equivalent to digit -3. After the reflection it's equivalent to digit 13, so I need to subtract 16. But I can equivalently do that by adding 16 before the reflection, and since 45 is the only relevant ASCII code not to have the bit corresponding to 16 set, I can accomplish that with a simple bitwise OR: 16|.
\$\endgroup\$
6
\$\begingroup\$

JavaScript, ES6, 27 bytes (37 - 10)

f=_=>_.replace(/\d/g,y=>y%5?10-y:5-y)

Try it in latest Firefox like

f("2983794-07")
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Perl port: Run s/\d/$&%5?10-$&:5-$&/ge with p flag like this: perl -pe's/\d/$&%5?10-$&:5-$&/ge' <(echo "2983794-07") which would be 23 + 1 - 10 = 14 bytes. \$\endgroup\$
    – hmatt1
    Oct 21, 2014 at 15:58
1
\$\begingroup\$

CJam, 13 (23 - 10) bytes

'-_l\/{{si_5%A5?\-}%}%*

Try it online here

\$\endgroup\$
0
\$\begingroup\$

C (63 - 10 = 53)

main(_){while(_=getchar())putchar(_>47?(_-=48)%5?58-_:53-_:_);}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.