31
\$\begingroup\$

(This is my first code-golf question)

When I was a child, my dad and I invented a game where the license plate we see on cars can give certain points based on some rather simple rules:

X amount of the same letter or number give X-1 points, examples:

22 = 1 point
aa = 1 point
5555 = 3 points

The numbers must be next to eachother, so 3353 only gives 1 point, as the 5 breaks the sequence of 3's.

A sequence of X numbers in ascending or descending order, at a minimum of 3, give X points, examples:

123 = 3 points
9753 = 4 points
147 = 3 points

The point system only works for 1-digit-numbers, so 1919 doesn't give points, and 14710 only give 3, (147).

Sequences can be combined to make more points, examples:

1135 = 4 points (1 point for 11 and 3 points for 135)
34543 = 6 points (3 points for 345 and 3 points for 543)

You are however not allowed to chop a larger sequence into 2 smaller sequences for extra points: 1234 = 123, 234 (6 points) is not allowed.

Your task is, given a sequence, to determine the number of points the license plate gives.

In Denmark, the license plates are structured like this: CC II III, where C is character and I is integer, and thus my example inputs will reflect this structure. If you wish, you may make the sequence fit your own structure, or, if you feel really adventurous, let the program analyze the structure of the license plate and thus have it work on any type of license plate around the world. Explicitly state the structure you decide to use in your answer though.

You may take the input in any way you please, either a string or an array seem to make most sense to me.

Test input | output:

AA 11 111 | 5
AB 15 436 | 3
OJ 82 645 | 0
UI 65 456 | 6
HH 45 670 | 5
YH 00 244 | 5
AJ 00 754 | 1

Due to the nature of choosing your own structure, or even covering all structures, I dont necessarily see how a winner can be explicitly determined. I suppose the winner will be the shortest bytes on the structure one has decided. (And don't take an input like C I C I C, just to make it easy for yourself)

EDIT:

Due to comments asking, I have a few extra pieces of information to share: A sequence of ascending or descending numbers refers to an arithmetic sequence, so X +/- a * 0, X +/- a * 1, ... X +/- a * n etc. So 3-5-7 for example is 3 + 2 * 0, 3 + 2 * 1, 3 + 2 * 2. The sequence does not, however, need to start from 0 nor end in 0.

MORE EDIT:

You may give the input in any way you please, you dont need to input spaces, dashes or any other thing that makes a license plate more readable. If you can save bytes by only accepting capital letters or something like that, you may do that aswell. The only requirement is that your program can take a string/array/anything containing both characters and numbers, and output the correct amount of points according to the rules stated.

\$\endgroup\$
  • \$\begingroup\$ Loosely related. Welcome to PPCG and nice first question! \$\endgroup\$ – Mr. Xcoder Sep 5 '17 at 10:01
  • \$\begingroup\$ Suggested test case: XX 87 654. I came up with something that was correct for all your test cases but somehow incorrect for this one.. Working on fixing it. \$\endgroup\$ – Kevin Cruijssen Sep 5 '17 at 11:35
  • 7
    \$\begingroup\$ I strongly suggest you fix a certain structure (I suggest CCIIIII, no spaces), or else this problem lacks an objective win criterion, which we require around here. As-is, “(And don't take an input like C I C I C, just to make it easy for yourself)” is very subjective. What is and isn’t an admissible structure? \$\endgroup\$ – Lynn Sep 5 '17 at 11:37
  • 1
    \$\begingroup\$ @Lynn An admissible structure is one that can actually yield points, CICIC will never have a sequence that yields any points, therefore it is not admissible. And on top of that, why is 'shortest answer in bytes on the chosen programming language and chosen structure' not a clear, objective win criteria? This win criteria has a straight, easy to follow rule, yet gives the developer freedom to choose what kind of structure they want it to work with. Granted it may have a lot of different winners, but, really, so what? \$\endgroup\$ – Troels M. B. Jensen Sep 5 '17 at 11:41
  • 3
    \$\begingroup\$ Test case: IA99999 (contains a decreasing sequence of code points, but not numbers). \$\endgroup\$ – Zgarb Sep 5 '17 at 13:10
7
\$\begingroup\$

05AB1E, 25 22 20 18 bytes

Accepts a string of lower-case alphabetic chars and numbers with no spaces.

Ç¥0Kγ€gXK>OIγ€g<OO

Try it online! or as a Test Suite

\$\endgroup\$
  • \$\begingroup\$ I can't read 05AB1E without an explanation ;) But can you save bytes by getting the input without spaces? \$\endgroup\$ – Kevin Cruijssen Sep 5 '17 at 10:27
  • \$\begingroup\$ @Mr.Xcoder I doubt it as well. But I personally can't read 05AB1E, so I thought perhaps Emigna had added any code to get rid of/ignore the spaces. Probably it does this implicitly without any extra bytes, but I just asked in case it didn't. \$\endgroup\$ – Kevin Cruijssen Sep 5 '17 at 10:31
  • \$\begingroup\$ I just took your code for a swing, and holy crap, it actually works for any length or sequence! The only 'issue' is that it also gives 3 points for ABC, which in and of itself isn't wrong, I just did not account for it, as in Denmark we only have 2 letters next to eachother. \$\endgroup\$ – Troels M. B. Jensen Sep 5 '17 at 10:40
  • 2
    \$\begingroup\$ @KevinCruijssen: No spaces in the input would save several bytes yes. I missed the part where we could decide that ourselves. Thanks for the heads up. (An explanation is coming as well). \$\endgroup\$ – Emigna Sep 5 '17 at 11:02
  • \$\begingroup\$ @Emigna I hadn't stated it in the question explicitly, I thought I hinted enough at it when I wrote You may take the input in any way you please, either a string or an array seem to make most sense to me.. \$\endgroup\$ – Troels M. B. Jensen Sep 5 '17 at 11:07
7
\$\begingroup\$

Husk, 20 16 15 bytes

-1 byte thanks to @Zgarb

Takes input without spaces and in lower case.

ṁ??¬o→LεL←gẊ¤-c

Try it online!

Explanation

           Ẋ      Map over all adjacent pairs
            ¤-c   get the difference of their codepoints
          g       Split into groups of equal elements
ṁ                 Map then sum
 ?       ←          If the head of the list is truthy (not 0)
  ?    ε              If the length of the list is 1
   ¬                    return 0
                       Else
    o→L                 return the length + 1
                     Else
        L             return the length
\$\endgroup\$
  • \$\begingroup\$ I think K0 can be ¬ here. \$\endgroup\$ – Zgarb Sep 5 '17 at 11:55
  • \$\begingroup\$ Hmm, this seems to fail on IA99999. \$\endgroup\$ – Zgarb Sep 5 '17 at 13:10
  • \$\begingroup\$ @Zgarb, changed input format to lower case. \$\endgroup\$ – H.PWiz Sep 5 '17 at 13:39
5
\$\begingroup\$

Python 3, 193 85 bytes

-3 bytes thanks to Lynn

Takes input as a byte-string with lowercase letters as: b'aa11111'.

def f(s):
 d=l=L=p=0
 for c in s:C=0!=d==c-l;p+=(c==l)+C*L;L=3>>C;d=c-l;l=c
 return p

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 131 bytes \$\endgroup\$ – Mr. Xcoder Sep 5 '17 at 12:45
  • 1
    \$\begingroup\$ C=0!=d==c-l is even shorter. \$\endgroup\$ – Lynn Sep 5 '17 at 13:31
2
\$\begingroup\$

Java 8, 195 bytes

a->{int r=a[0]==a[1]?1:0,i=3,j,p=a[2],x,y,z;for(;i<7;p=a[i++])for(r+=(x=a[i])==p?1:0,j=-4;++j<4;r+=j==0?0:i<6&&p+j==x&x+j==(y=a[i+1])?++i<6&&y+j==(z=a[i+1])?++i<6&&z+j==a[i+1]?5:4:3:0);return r;}

Can definitely be golfed some more by using another technique to check for sequences.

Explanation:

Try it here.

a->{                      // Method with character-array parameter and integer return-type
  int r=                  //  Result-integer
        a[0]==a[1]?       //   If the two letters are equal:
         1                //    Start this result-integer at 1
        :                 //   Else:
         0,               //    Start the result-integer at 0 instead
      i=3,j,              //  Index-integers
      p=a[2],x,y,z;       //  Temp integers
   for(;i<7;              //  Loop (1) from index 3 to 7 (exclusive)
       p=a[i++])          //    And after every iteration: Set `p` and raise `i` by 1
     for(r+=(x=a[i])==p?  //   If the current digit (now `x`) equals the previous `p`:
             1            //    Raise the result-integer by 1
            :             //   Else:
             0,           //    Keep the result-integer the same
         j=-4;++j<4;      //   Inner loop (2) from -3 to 3 (inclusive)
       r+=j==0?           //    If `j` is 0:
           0              //     Skip it, so keep the result-integer the same
          :i<6            //    Else-if `i` is not 6,
           &&p+j==x       //    and the previous digit `p` + `j` equals the current digit,
           &x+j==(y=a[i+1])?
                          //    and the current digit `x` + `j` equals the next digit `y`:
            ++            //     Raise index `i` by 1 first,
              i<6         //     and check if `i` is not 6 again,
              &&y+j==(z=a[i+1])?
                          //     and if the new current digit `y` + `j` equals the next digit `z`:
               ++         //      Raise index `i` by 1 first again,
                 i<6      //      and check if `i` is not 6 again,
                 &&z+j==a[i+1]?
                          //      and if the new current digit `z` + `j` equals the next digit:
                  5       //       Raise the result-integer by 5
                 :        //      Else:
                  4       //       Raise it by 4 instead
              :           //     Else:
               3          //      Raise it by 3 instead
           :              //    Else:
            0             //     Keep it the same
     );                   //   End of inner loop (2)
                          //  End of loop (1) (implicit / single-line body)
  return r;               //  Return the result-integer
}                         // End of method
\$\endgroup\$
1
\$\begingroup\$

Pyth,  51  50 bytes (staggering)

+/|R0.+QZslM.MlZ+kfTsmm?&!-K.+sMkhK.AKkY.:>Q2d}3 5

Verify all test cases or Try it here.

\$\endgroup\$
  • \$\begingroup\$ I now realize I can make it shorter, but will update if I am able to golf it consistently. \$\endgroup\$ – Mr. Xcoder Sep 5 '17 at 12:00
1
\$\begingroup\$

R, 153, 145, 143 bytes

function(x){p=0;s=sum;if(x[1]==x[2])p=1;a=diff(strtoi(x[3:7]));p=p+s(a==0);l=sort(table(a[a!=0]),T);(p=p+s(l[(l[((s(l)>0)&(l[1]>1))]+1)>2]+1))}

Anonymous function that takes a character vector and returns an integer.
Expected input z(c("A", "A", "1", "1", "1", "1", "1"))

Try it online!

Ungolfed version

function(x){
  pnt <- 0; s <- sum
  if(x[1] == x[2]) pnt <- 1
  a <- diff(strtoi(x[3:7]))
  pnt <- pnt + s(a == 0)
  l <- sort(table(a[a!=0]), T)
  (pnt <- pnt + s(l[(l[((s(l) > 0) & (l[1] > 1))] + 1) > 2] + 1))
}
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 91 bytes

f(p,r,d,D,s,l)char*p;{for(r=D=0;d=p[1]-*p,*++p;D=d){s=d&&d==D;r+=!d+s*l;l=3-2*s;}return r;}

Try it online!

Idea stolen from Felipe Nardi Batista's Python answer.

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

Pyth, 48 42 bytes

Direct port from my python answer. Takes input as a byte-string with lowercase letters as: b'aa11111'.

This is my first time coding in Pyth, so any tips are welcome :D

KJ=b0VQ=d&KqK-NJ=+b+qNJ*dZ=Z-3yd=K-NJ=JN;b

Try it here

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

JavaScript, 216 192 186 202 201 bytes

function f(s){var a=s.split(" "),c=a[1],a=a[0],r,h=b=i=p=0;for(i=0;i<4;i++){if(i<2&(r=a[i+1]-a[i]==a[i+2]-a[i+1])){p++;b=2}if(i>0){if(a[i]==a[i-1]){p++;h++}if(i<3&c[i]==c[i-1])p++}}return h==4?p+b:p-b}

Unminified

function f(s){
    var a=s.split(" "),c=a[1],a=a[0],r,h=b=i=p=0;
    for(i=0;i<4;i++){
        if(i<2&(r=a[i+1]-a[i]==a[i+2]-a[i+1])){
            p++;
            b=2
        }
        if(i>0){
            if(a[i]==a[i-1]){
                p++;
                h++;
            }
            if(i<3&c[i]==c[i-1]) 
                p++;
        }
    }

    return h==4?p+b:p-b
}

Edit history:

  • Narrowed down the code to only work with 0000 XXX format. (-24 bytes)
  • Edits as suggested by @Titus. (-6 bytes)
  • fixed a bug where four identical numbers gave a score of 7 instead of 3. (+16 bytes)
  • Removed last semi-colon. (-1 byte)
  • Fixed a typo in the code. (no byte change)
\$\endgroup\$
  • \$\begingroup\$ How do i count the bytes? \$\endgroup\$ – Brian H. Sep 12 '17 at 8:09
  • 1
    \$\begingroup\$ Byte counter \$\endgroup\$ – H.PWiz Sep 12 '17 at 8:12
  • \$\begingroup\$ im really hating the fact that the code block isn't recognizing the language... \$\endgroup\$ – Brian H. Sep 12 '17 at 8:14
  • \$\begingroup\$ Do want syntax highlighting? \$\endgroup\$ – H.PWiz Sep 12 '17 at 8:15
  • \$\begingroup\$ btw, 0000 gives 7 points, is that correct? (it's getting read as an arithmetic sequence and a repeated number sequence at the same time) \$\endgroup\$ – Brian H. Sep 12 '17 at 8:24

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.