5
\$\begingroup\$

Introduction

So my boss is making me do some manual labor...urgh...I have to take a part out a box of parts, process it and put it back in the box. To make this more interesting I try to sort the parts as I do this, as each part has a unique alphanumeric serial number.

Unfortunately, I only have room to have four parts out the box at a time and I'm not taking each part out of the box more than once (that'd be even more work!) so my sorting capabilities are limited.

Challenge

Your challenge is to write a full program or function that rates my sorting of a given box of parts.

The Sorting Process

  1. Take 4 parts out of the box
  2. Take the part that comes first by serial number
  3. Process the part
  4. Put the part back in the box
  5. Take another part out of the box (if there are any left)
  6. Repeat steps 2-6 until all parts are processed (moved only once).

Input

A set of serial numbers in the format [A-Z][A-Z][0-9][0-9][0-9] Input can be an array/list or deliminated string, python example...

['DT064', 'SW938', 'LB439', 'KG338', 'ZW753', 'FL170', 'QW374', 'GJ697', 'CX284', 'YW017']

Output:

The score of the sorted/processed box (see scoring below)

Scoring

For each part note how far away it is from where it would be in a perfectly sorted box. The score is the total of this value for all parts in the box.

Examples/Test cases

input          ['AA111', 'AA222', 'AA333', 'AA444', 'AA000', 'AA555']
my sorted list ['AA111', 'AA000', 'AA222', 'AA333', 'AA444', 'AA555'] (no need to output)
perfect sort   ['AA000', 'AA111', 'AA222', 'AA333', 'AA444', 'AA555'] (no need to output)
output         2 (because first two elements are out of sequence by 1 position each)

input          ['GF836', 'DL482', 'XZ908', 'KG103', 'PY897', 'EY613', 'JX087', 'XA056', 'HR936', 'LK200']
my sorted list ['DL482', 'GF836', 'EY613', 'JX087', 'KG103', 'HR936', 'LK200', 'PY897', 'XA056', 'XZ908']
perfect sort   ['DL482', 'EY613', 'GF836', 'HR936', 'JX087', 'KG103', 'LK200', 'PY897', 'XA056', 'XZ908']
output         6

input  ['UL963', 'PJ134', 'AY976', 'RG701', 'AU214', 'HM923', 'TE820', 'RP893', 'CC037', 'VK701']
output 8

input  ['DE578', 'BA476', 'TP241', 'RU293', 'NQ369', 'YE849', 'KS028', 'KX960', 'OU752', 'OQ570']
output 6

input  ['BZ606', 'LJ644', 'VJ078', 'IK737', 'JR979', 'TY181', 'RJ990', 'ZQ302', 'AH702', 'EL539']
output 18
\$\endgroup\$
  • \$\begingroup\$ I have an example solution (ungolfed) in Python 3 if needed. \$\endgroup\$ – Notts90 Jun 9 '17 at 10:13
  • \$\begingroup\$ How does AA000 come after AA111? \$\endgroup\$ – Leaky Nun Jun 9 '17 at 10:17
  • \$\begingroup\$ Also, you said that the score is the sum of the distances. Wouldn't that make the first example's output significantly greater than 2? \$\endgroup\$ – Leaky Nun Jun 9 '17 at 10:18
  • \$\begingroup\$ @LeakyNun in the first example AA000 didn't come out the box until the second iteration so the best position it could achieve is second in the sorted box. I've added a bit more explanation to the test case. \$\endgroup\$ – Notts90 Jun 9 '17 at 10:20
  • \$\begingroup\$ Can you please post 1 or 2 examples of "my sorted lists" in the test cases? \$\endgroup\$ – J42161217 Jun 9 '17 at 11:55
3
\$\begingroup\$

05AB1E, 28 25 24 23 bytes

-5 bytes thanks to Emigna

gÍG4ôć{ćˆì˜}¯ì©{vXykNαO

Try it online!

Explanation

gÍG4ôć{ćˆì˜}¯ì©{vXykNαO   Argument l
gÍG        }              len(l)-3 times do:
   4ô                       Split into pieces of 4
     ć                      Extract head
      {                     Sort
       ćˆ                   Extract head and add to global array
         ì˜                 Put rest of array back together
            ¯ì            Prepend global array to leftover elements
              ©           Store in register_c without popping
               {          Sort
                v         For y in sorted array, do:
                 ®yk        Index of y in register_c
                    Nα      Absolute difference with iteration index
                      O     Total sum of index differences
\$\endgroup\$
  • \$\begingroup\$ Haven't really checked the challenge, but you could save 3 bytes like this: gÍG4ôć{ćˆì˜}¯ìUI{vXykNα}O. \$\endgroup\$ – Emigna Jun 9 '17 at 11:18
  • \$\begingroup\$ @Emigna Thanks I didn't know arrays could be stored in X. I will update it \$\endgroup\$ – kalsowerus Jun 9 '17 at 11:23
  • \$\begingroup\$ Actually the final } could be omitted as well. \$\endgroup\$ – Emigna Jun 9 '17 at 11:26
  • \$\begingroup\$ In total 5 bytes saved as gÍG4ôć{ćˆì˜}¯ì©{v®ykNαO. Using the register and sorting the final list instead of input (as they contain the same items anyways) \$\endgroup\$ – Emigna Jun 9 '17 at 11:28
  • \$\begingroup\$ @Emigna I was sure the last part could be optimised, thanks! \$\endgroup\$ – kalsowerus Jun 9 '17 at 11:33
2
\$\begingroup\$

JavaScript (ES6), 104 bytes

a=>a.reduce(p=>(j=i-[...a].sort().indexOf(a[a.splice(i,4,...a.slice(i,i+4).sort()),i++]))>0?p+j:p-j,i=0)

Test cases

let f =

a=>a.reduce(p=>(j=i-[...a].sort().indexOf(a[a.splice(i,4,...a.slice(i,i+4).sort()),i++]))>0?p+j:p-j,i=0)

console.log(f(['AA111', 'AA222', 'AA333', 'AA444', 'AA000', 'AA555']))
console.log(f(['GF836', 'DL482', 'XZ908', 'KG103', 'PY897', 'EY613', 'JX087', 'XA056', 'HR936', 'LK200']))
console.log(f(['UL963', 'PJ134', 'AY976', 'RG701', 'AU214', 'HM923', 'TE820', 'RP893', 'CC037', 'VK701']))
console.log(f(['DE578', 'BA476', 'TP241', 'RU293', 'NQ369', 'YE849', 'KS028', 'KX960', 'OU752', 'OQ570']))
console.log(f(['BZ606', 'LJ644', 'VJ078', 'IK737', 'JR979', 'TY181', 'RJ990', 'ZQ302', 'AH702', 'EL539']))

\$\endgroup\$
  • \$\begingroup\$ I now see that my use of shift led me down a dead end, although I still would only have ended up with a 105-byte answer at best, since I wouldn't have used the i=0 trick either. \$\endgroup\$ – Neil Jun 9 '17 at 14:01
1
\$\begingroup\$

JavaScript (ES6), 115 113 bytes

a=>a.reduce((r,_,i)=>r+=(i-=[...a].sort().indexOf((b=b.splice(0,4).sort().concat(b)).shift()))<0?-i:i,0,b=[...a])
\$\endgroup\$
1
\$\begingroup\$

PHP, 132 bytes

<?for($d=4+count($p=$g=$_GET),sort($p);--$d;rsort($n),$i<4?:$r+=abs($k++-array_flip($p)[array_pop($n)]))$d<4?:$n[]=$g[+$i++];echo$r;

Try it online!

PHP, 136 bytes

<?for($n=array_slice($p=$g=$_GET,0,$i=3),sort($p);$n;rsort($n),$r+=abs($k++-array_flip($p)[array_pop($n)]))$g[$i]&&$n[]=$g[$i++];echo$r;

Try it online!

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

Mathematica, 173 bytes

A={};(V=Sort[S=#];While[Length@S>4,T=Take[Sort@S[[;;4]],1][[1]];A~AppendTo~T;S=S~DeleteCases~T];Tr@Table[Abs[i-Flatten[Position[Join[A,Sort@S],V[[i]]]][[1]]],{i,Length@V}])&


input

[{BZ606, LJ644, VJ078, IK737, JR979, TY181, RJ990, ZQ302, AH702, EL539}]

output

18

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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