11
\$\begingroup\$

Today, my girlfriend and I started with this year's Advent of Code to improve our Python. So far we only did Day 1 and the first part of Day 2. The latter, I found to be very fun, so, to spice it up a little, and with a tip o' the head to Eric Wastl, here's my attempt at turning it into a Code Golf challenge.

Task

Your task, should you accept it, would be to determine the number of lines where the number of occurrences of char in each string is at least min and at most max. With the above data, a valid entry will output 23¹.

Valid entries shall not only work with the below two example data sets, but with any data set produced by the Advent of Code website's Day 2 Part 1 puzzle (so head on over there already - but just going with the examples is fine, too). Other formats of input are not allowed. Specifically, the entry has to handle the newlines itself.

I've posted an answer with my Ruby version along with this Question in the hopes that it will help.

Input

  • A multiline string where each line/element contains a number min, a number max, a character char, and a string password² (i.e., min-max char: password).
  • min and max are positive integers, and max is guaranteed to be greater than or equal to min.
  • As far as I can tell, the passwords are all ASCII-alphabetical, as is the char.

Output

  • A number representing how many lines had at least min and at most max occurrences of char in password.

Scoring

  • The winner shall be the shortest entry by characters³, not bytes (to make sure that variable names such as 🎅, 🦌 and 🎄 don't put contestants in the mood of the season at a disadvantage).

(Shorter) Test Case

Output: 3
Input:
1-5 c: abcdefg
5-12 j: abcdefg
1-5 z: zzzzzzzz
3-3 h: hahaha
4-20 e: egejeqwee

(Longer) Example

Output: 23
Input:
3-8 j: ksjjtvnjbjppjjjl
6-10 s: sszlkrsssss
1-4 z: znzfpz
7-11 m: dfkcbxmxmnmmtvmtdn
6-9 h: hlhhkhhhq
8-9 p: ppppppppvp
6-9 c: cccgccccmcch
5-8 g: bgkggjgtvggn
3-4 x: xxhk
10-12 h: hhhhhhhhhlhhhhdnh
5-6 j: zjjsjn
9-14 s: ksclwttsmpjtds
6-7 l: gtdcblql
1-4 d: dkndjkcd
1-6 k: kkkkkckmc
10-12 x: xxxxxxxxxrxl
2-5 v: fxdjtv
3-5 q: pjmkqdmqnzqppr
5-8 t: xtgtgtcht
6-11 j: nftjzjmfljqjrc
6-9 g: rskgggmgmwjggggvgb
11-12 v: vkvdmvdvvvdv
15-16 z: zzzzzzzzzzzzzzgxz
13-17 v: vvvvvvvvvvvvvvvvnv
9-14 j: clrfkrwhjtvzvqqj
1-12 l: lljljznslllffhblz
4-7 c: cccgcxclc
6-11 z: tbxqrzzbwbr
18-19 m: jmbmmqmshmczlphfgmf
1-5 s: ssvsq
4-5 k: kkkkw
4-7 f: fcqffxff
3-8 f: fffzthzf
9-12 v: vvvvvvvvvvvmvv
14-17 f: ftflcfffjtjrvfkffmvf
4-7 n: pnmnxnqqjp
6-7 r: rrrkrmr
4-13 k: drckdzxrsmzrkqckn
5-14 t: dttttttttttttt
13-14 v: vvvvfvvvvvvvvvv
7-12 j: jrljwnctjqjjxj
6-7 v: wchfzvm
1-8 d: dddddddzd
10-12 g: gvvzrglgrgggggggggg

¹: But, say puts 23 (that is, without doing the actual work) is no valid entry.
²: On Advent of Code, the background to the puzzle is that you're helping a toboggan rental company with their computer trouble. For some reason, their password database has been jumbled up and you're trying to fix it, which requires you to figure out which passwords adhere to the password policy, which solely consists of a specific character (char) to be present at least and at most a certain number of times.
³: As determined via wc -m.

\$\endgroup\$
18
  • 3
    \$\begingroup\$ Welcome to Code Golf! It's recommended to post challenges in the Sandbox so people can help you with it. For example, the input and output here is a little unclear - do you want us to output truthy/falsy values for each line, or the number of lines where the number of occurrences of the character is between min and max? Also, reading from a file is a rather strict input format - some languages can't do that, you can't really test it online, and it's just inconvenient. \$\endgroup\$
    – user
    Dec 5 '20 at 19:37
  • 6
    \$\begingroup\$ In addition to @user's comments, scoring solutions by bytes rather than characters is one of our strongest rules here; it's rarely overriden and, when it is, there's usually a very good reason for it, which, unless I've missed something, I don't see here. \$\endgroup\$
    – Shaggy
    Dec 5 '20 at 19:42
  • 3
    \$\begingroup\$ I've modified your question a bit to hopefully make it easier to read and make the task clearer. Feel free to roll it back or improve/remove some of the changes I've made \$\endgroup\$
    – user
    Dec 5 '20 at 20:02
  • 4
    \$\begingroup\$ You should be more explicit about whether we are required to take the input in the exact format Advent of Code provides it in, or we are allowed to use any reasonable format, given I've seen both kinds of answer. \$\endgroup\$ Dec 5 '20 at 20:37
  • 3
    \$\begingroup\$ @Xcali This was discussed before in since-deleted comments. I argued that twitter.com/ericwastl/status/699455417768923137 from 2016 constitutes sufficient permission. \$\endgroup\$ Dec 6 '20 at 4:46

21 Answers 21

8
\$\begingroup\$

Ruby, 56 characters

eval'㵰␼⹣潵湴筼汼氽縯層⬠⠮⤯㬨①⹴潟椮⸤☮瑯彩⤽㴽␧⹣潵湴⠤ㄩ紻'.encode('utf-16').b

Try it online!

Decompresses to:

p$<.count{|l|l=~/\d+ (.)/;($`.to_i..$&.to_i)===$'.count($1)}

Character scoring allows these sort of shenanigans!

This is also a great showcase of Ruby's regex global variables. Given a line:

3-8 j: ksjjtvnjbjppjjjl

Our regex matches here:

3-8 j: ksjjtvnjbjppjjjl
  \-/
  Regex matches here

Then $' is the part after the regex, $& is the part that matches the regex, $` is the part before the regex, and $1 matches the character to count, so we have:

$` = '3-'
$& = '8 j'
$1 = 'j'
$' = ': ksjjtvnjbjppjjjl'

Since Ruby's to_i just ignores garbage after a numeric string, we can just cast it to int.

Ruby -n, 60 characters

~/\d+ (.)/;($`.to_i..$&.to_i)===$'.count($1)||$.-=1
END{p$.}

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Fiiiiiiinally some Unicode going on here! Still waiting for the use of christmas symbols, though :-D \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 2:07
  • 1
    \$\begingroup\$ @Sixtyfive The Christmas message is ☮ on earth and goodwill to all, does that count? \$\endgroup\$
    – Neil
    Dec 6 '20 at 11:29
  • \$\begingroup\$ Sure, @Neil, if you make that into an Answer using ☮ and 🌍! (And I'm also still hoping for an Answer using 🎅, 🦌 and 🎄!) \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 14:15
  • \$\begingroup\$ @Sisyphus thank you for adding the explanations about what's going on. That was a great St. Nick day's gift! \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 14:19
  • 1
    \$\begingroup\$ It's funny how, because of all the fullwidth ideograms, the Unicode compressed program is visually longer than what it evals \$\endgroup\$ Dec 7 '20 at 4:15
6
\$\begingroup\$

Python 3, 71 characters

exec(bytes("浩潰瑲爠੥㵦慬扭慤砠攺慶⡬敲献扵✨尨⭤⸩尨⭤⸩⸨⠩⬮尩㽮Ⱗ❲⠫ㅜ㴼尢∴挮畯瑮∨㍜⤢㴼㉜✩砬⤩",'U16')[2:])

Try it online!

Decompresses to:

import re
f=lambda x:eval(re.sub('(\d+).(\d+).(.)(.+)\n?',r'+(\1<="\4".count("\3")<=\2)',x))

Note we need to name the function even though it's not recursive since we are wrapping in exec.

This is probably not optimal, since we shouldn't have to use regexes here, but parsing the format is so painful using only Python stdlib.

\$\endgroup\$
3
  • \$\begingroup\$ A twinkly start! That's a good start! \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 2:08
  • \$\begingroup\$ ok wait, how'd you squish the normal python code into a bunch of chinese characters and arrows and things? \$\endgroup\$
    – matt
    Dec 7 '20 at 16:56
  • \$\begingroup\$ @matt Try it online! \$\endgroup\$
    – Sisyphus
    Dec 7 '20 at 21:26
6
\$\begingroup\$

GAWK, 87 bytes

split($1,b,"-")sub(":",a,$2){n=b[1];x=b[2];c=split($3,y,$2);t+=n<c&&--c<=x}END{print t}

Try it online!

It's pretty brute force, but there are a few GAWK'isms that shorten things a bit.

First, doing the "split" and "sub" calls in the test condition section means not having to use ; characters.

Then taking advantage of the fact that uninitialized variables are treating like empty strings (when strings are called for), means it’s possible to use a instead of "" and save one character.

The code also relies on the fact that when a uninitialized variable is treated like an integer, its value is 0

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Welcome to the site! Consider using Try it online! to demonstrate your answer (I'm using the shorter example so that the link fits in this comment but there is also has an option to provide CGSE markdown boilerplate). \$\endgroup\$
    – Neil
    Dec 6 '20 at 11:17
  • \$\begingroup\$ Yay, another non-golfing language! :-D \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 14:13
  • \$\begingroup\$ Thanks for the "try it online" info! Added... \$\endgroup\$
    – cnamejj
    Dec 6 '20 at 21:50
6
\$\begingroup\$

05AB1E, 16 bytes

|ʒ#ć'-¡ŸsR`н¢å}g

Try it online!

This is technically a character counter with a twist!

-2 bytes thanks to ovs!

At the time of writing, this answer is winning!

How?

|                     # Convert the input into a list
 ʒ#                   # For each element in each line
   ć'-¡Ÿ              # Parse the range
        sR`           # Unpack the string and character
           н          # Parse the character
            ¢å        # Is the number of occurences in the range?
              }g      # How many lines have this condition?
\$\endgroup\$
6
  • \$\begingroup\$ Can you still be the winning entry in the Golfing Languages category while also splitting the string at newlines yourself? :) \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 14:12
  • \$\begingroup\$ Yes, of course. It can be done by inserting a vertical bar (|) at the start of the program, but taking input as a list is allowed by default. \$\endgroup\$
    – SunnyMoon
    Dec 6 '20 at 15:52
  • 5
    \$\begingroup\$ This chalelnge explicitly overrides the default by saying "Specifically, the entry has to handle the newlines itself." \$\endgroup\$ Dec 6 '20 at 16:13
  • 2
    \$\begingroup\$ 16 bytes using ` and less swapping. \$\endgroup\$
    – ovs
    Dec 6 '20 at 21:19
  • 1
    \$\begingroup\$ I also tried to abuse the scoring method, but this program is a little too short already. \$\endgroup\$
    – ovs
    Dec 6 '20 at 21:38
5
\$\begingroup\$

Jelly, 25 19 bytes=chars

ḲµḢ⁾-ryViḢċ@¥/
ỴÇƇL

Try it online!

ḲµḢ⁾-ryViḢċ@¥/    Monadic helper link:
 µ                Consider
Ḳ                 the argument split on spaces.
  Ḣ               Pop the first element,
   ⁾-ry           replace the hyphen with "r" (inclusive range dyad),
       V          and eval.
        i         Find the index (or 0 if not found) in that range of
          ċ       the number of occurrences of
         Ḣ @      the first element of
            ¥/    the first remaining element of the split input in the second.

ỴÇƇL              Main link:
Ỵ                 split on newlines,
  Ƈ               filter by
 Ç                helper link (is the password valid?),
   L              length.
\$\endgroup\$
0
5
\$\begingroup\$

Ruby, 80 74 characters, thanks to pppery

p $<.count{|l|a,b,c,s=l.split /[- ]/;a.to_i>=x=s.count(c[0])and x<=b.to_i}

Try it online

or if you dislike "magic" numbers:

p $<.count{|l|a,b,c,s,s=l.split /[- :]/;a.to_i>=x=s.count(c)and x<=b.to_i}

Try it online

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Didn't know about the ?string syntax, thank you @Efi! :-) \$\endgroup\$
    – Sixtyfive
    Dec 5 '20 at 20:38
  • \$\begingroup\$ 74 characters \$\endgroup\$ Dec 5 '20 at 20:44
  • \$\begingroup\$ Piping it from stdin is alright, so long as it remains a string. FWIW, I've worked that same suggestion into my original answer, too. \$\endgroup\$
    – Sixtyfive
    Dec 5 '20 at 21:05
  • \$\begingroup\$ 73 p$<.count{|l|a,b,c,_,s=l.split /\W/;a.to_i>=x=s.count(c[0])and x<=b.to_i} \$\endgroup\$
    – JJoos
    Dec 7 '20 at 11:34
  • \$\begingroup\$ 67 p$<.count{|l|a,b,c,s=l.split /-|:? /;(a.to_i..b.to_i)===s.count(c)} \$\endgroup\$
    – Efi
    Dec 19 '20 at 20:09
4
\$\begingroup\$

Python 3, 121 characters

def f(x,z=0):
 for l in x.splitlines():n,p=l.split(":");m,o=n.split("-");z+=int(m)<=p.count(o[-1])<=int(o[:-1])
 return z

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ I think you can input directly as a list (OP didn't remove my changes allowing that) \$\endgroup\$
    – user
    Dec 5 '20 at 20:30
  • 2
    \$\begingroup\$ My reading of the challenge, even with your edit, didn't make that clear, so I went with taking the string directly. \$\endgroup\$ Dec 5 '20 at 20:31
  • \$\begingroup\$ Already learned some new Python now, thanks for that! :-) \$\endgroup\$
    – Sixtyfive
    Dec 5 '20 at 21:04
4
\$\begingroup\$

Oracle SQL, 231/186/117 bytes

with p as (select regexp_substr(s,'\d+') l,
regexp_substr(s,'\d+',1,2) u,
regexp_substr(s,'\w+',1,3) j,
regexp_substr(s,'\w+',1,4) p from test)
select count(*) from p where length(regexp_replace(p,'[^'||j||']','')) between l and u;

Try it online!

The obvious shortening takes it down to 186 bytes with a marked decrease in legibility.

select count(*) from test where length(regexp_replace(regexp_substr(s,'\w+',1,4),'[^'||regexp_substr(s,'\w+',1,3)||']','')) between regexp_substr(s,'\d+') and regexp_substr(s,'\d+',1,2);

Using regexp_replace to construct a regular expression to match the full input takes us down to 117 bytes.

select count(*) from test where regexp_like(s,regexp_replace(s,'(\w+)-(\w+) (\w): \w+',':[^\3]+(\3[^\3]*){\1,\2}$'));

The regular expression is:

: start at the colon
[^\3]+ read in the space and any other characters not the key
(\3[^\3]*) the key character followed by any number of non-key characters
{\1,\2} exactly lowerbound-upperbound times
$ end of string
\$\endgroup\$
3
\$\begingroup\$

Scala, 84 characters

_ split "\n"count{case s"$m-$n $c:$s"=>val o=s.count(c(0)==_);m.toInt<=o&o<=n.toInt}

Try it online

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The OP has clarified that you have to use AOC's exact format. \$\endgroup\$ Dec 5 '20 at 20:42
  • \$\begingroup\$ @pppery Thanks for the heads-up \$\endgroup\$
    – user
    Dec 5 '20 at 20:44
3
\$\begingroup\$

Ruby, 138 130 characters, thanks to pppery

n=0;$<.readlines.each{|s|i,a,c,p=s.match(/(\d+)-(\d+) (.): (.*)/).to_a.drop(1);m=p.count(c);n+=1 if (m>=i.to_i&&m<=a.to_i)};puts n

Try it online

\$\endgroup\$
7
  • 3
    \$\begingroup\$ You shouldn't answer you own challenges for at least a few days to give others a chance. You also have the advantage in knowing the challenge before hand. \$\endgroup\$
    – Noodle9
    Dec 5 '20 at 19:56
  • 2
    \$\begingroup\$ @Noodle9 "knowing the challenge before hand" doesn't really apply since lots of people here did it as part of AOC. \$\endgroup\$ Dec 5 '20 at 20:06
  • 2
    \$\begingroup\$ 130 characters \$\endgroup\$ Dec 5 '20 at 20:10
  • 1
    \$\begingroup\$ Sorry, @Noodle9, I was more than certain that I won't be able to compete even in the slightest. From what I've seen here, it's common for people to use languages purposefully invented just for golfing, so I saw this more as a the-code-is-the-documentation/courtesy thing than as a serious entry of my own. \$\endgroup\$
    – Sixtyfive
    Dec 5 '20 at 20:23
  • 3
    \$\begingroup\$ Tradition is to edit golfing suggestions like the one I gave into the answer, striking out the previous bytecount and often leaving an annotation like "-8 bytes thanks to pppery" \$\endgroup\$ Dec 5 '20 at 20:27
3
\$\begingroup\$

Retina, 43 bytes

%~`(.+)-(.+) (.): .+
[^$3]¶¶^$3$3{$1,$2}$
1

Try it online! Explanation:

%`

Loop over each line of the input.

(.+)-(.+) (.): .+
[^$3]¶¶^$3$3{$1,$2}$

Write a script that deletes everything other than the desired character, then matches the required range of characters, plus one (for the character itself).

~`

Evaluate that script on the line of input.

1

Sum the number of successful matches.

As an example, the script for the line 6-10 s: sszlkrsssss is

[^s]

^ss{6,10}$

The first stage deletes everything except s, leaving ssssssss (1 for the s itself and 7 occurrences in the string). The second stage then checks that there are between 7 and 11 occurrences.

\$\endgroup\$
2
  • \$\begingroup\$ Is "7 and 11" a typo for "6 and 10"? \$\endgroup\$ Dec 6 '20 at 1:48
  • \$\begingroup\$ @pppery No, ^ss{6,10}$ matches between 7 and 11 ss, one for the original s character and 6-10 for the desired ss in the string. \$\endgroup\$
    – Neil
    Dec 6 '20 at 11:07
3
\$\begingroup\$

JavaScript (ES6),  89 ... 84  83 bytes / chars

s=>s[S='split']`
`.map(s=>t+=([m,M,c]=s[S](/\W/),k=s[S](c).length-2)>=m&k<=M,t=0)|t

Try it online!

Commented

s =>                  // s = input string
s[S = 'split']`\n`    // split it on line-feeds
.map(s =>             // for each line s:
  t +=                //   update t:
    ( [m, M, c] =     //     [m, M, c] = [min, max, char] obtained by ...
      s[S](/\W/),     //     ... splitting s on non-alphanumerical characters
      k = s[S](c)     //     k = number of occurrences of c in s, minus 1
          .length - 2 //         (so we have to subtract 2 from the length of the array)
    ) >= m &          //     increment t if k is greater than or equal to m
    k <= M,           //     and less than or equal to M
  t = 0               //   starting with t = 0
) | t                 // end of map(); return t
\$\endgroup\$
5
  • 1
    \$\begingroup\$ The asker has just edited the question to say that "Specifically, the entry has to handle the newlines itself", invalidating this answer. \$\endgroup\$ Dec 5 '20 at 21:25
  • 1
    \$\begingroup\$ @pppery But it still says "A multiline string or list" as input. Or am I misunderstanding which newlines we're talking about? \$\endgroup\$
    – Arnauld
    Dec 5 '20 at 21:45
  • 1
    \$\begingroup\$ @pppery Ok, the challenge has now been clarified some more. Fixed accordingly. \$\endgroup\$
    – Arnauld
    Dec 5 '20 at 22:11
  • 2
    \$\begingroup\$ It's funny packing it 2:1 is the exact same length, 87 chars: eval(unescape(escape`𬰽🡳𛡲𩑰𫁡𨱥𚀯𚁜𩀪𚐭𚁜𩀪𚐨𛠪𚐯𩰬𚁟𛁭𛁍𛁳𚐽🡴𚰽𚁫👳𛡳𬁬𪑴𚁳𦰱𧐩𛡬𩑮𩱴𪀭𜠩🠽𫐦𪰼👍𛁴🐰𚑼𭀠`.replace(/u../g,''))) \$\endgroup\$
    – Sisyphus
    Dec 6 '20 at 1:50
  • \$\begingroup\$ I'll take the man with the turban, too. Let's call him one of the three Magi! \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 2:09
3
\$\begingroup\$

Wolfram Language (Mathematica), 90 characters

Count[FromDigits/@(#≤{#5~StringCount~#3}≤#2)&@@@S[#~S~"
","-"|" "|":"],1>0]&
S=StringSplit

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Pip -r, 21 characters

$+Y{a<=cNd<=b}MUg@+XW

Try it online!

Explanation

$+Y{a<=cNd<=b}MUg@+XW
                       g is lines of stdin (due to -r flag); XW is builtin for regex \w
                  +XW  Construct the regex \w+
                g@     Get all matches in each element of list g: this converts each line
                        into a list containing the two numbers, the char, and the password
   {         }MU       Map this function, calling it on the four elements of each sublist:
    a                   The first number
     <=                 is less than or equal to
       cNd              the number of occurrences of the char in the password
          <=            which is less than or equal to
            b           the second number
                       The results are 1 if the condition is true, 0 otherwise
$+Y                    Fold the list of 1s and 0s on addition (the Y is just there to make
                        this operation lower precedence than MU)

As a bonus, here's a 29-character port of Sisyphus' elegant Ruby solution using regex match variables:

{a~XI.s.CXX$`<=$1N$'<=-$0}MSg

Try it online!

Explanation:

{                        }MSg  Map this function to each line and sum the results:
   XI.s.CXX                     Construct this regex: -?\d+ (.)
 a~                             Find the first match in the line (everything from the
                                 hyphen through the char)
           $`                   The part left of the match (the first number)
             <=                 is less than or equal to
                 N              the number of occurrences of
               $1               capture group 1 (the char) in
                  $'            the part right of the match (the password, with an extra
                                 colon and space that don't matter)
                    <=          which is less than or equal to
                      -$0       the whole match, treated as a number, negated (the
                                 second number)
\$\endgroup\$
1
  • \$\begingroup\$ Nice to see Pip nearly at par with the other golflangs here. \$\endgroup\$
    – Razetime
    Dec 7 '20 at 4:39
2
\$\begingroup\$

Charcoal, 35 bytes

WΦ⪪S κ«≔I⪪§ι⁰-θ⊞υ№…·⌊θ⌈θ№⊟ι§⊟ι⁰»IΣυ

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

WΦ⪪S κ«

Loop over the input, splitting each line on spaces.

≔I⪪§ι⁰-θ

Extract the numbers from the first part of the string.

⊞υ№…·⌊θ⌈θ№⊟ι§⊟ι⁰

Create a range across those numbers and check whether the count of characters in the string occurs within the range.

»IΣυ

Output the total number of matches.

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 56 characters

Run with perl -nE. Takes input in the usual Perl ways: as standard input or as filenames passed as command line args.

/-(.+) (.):/;{$x=()=/$2/g};$i++if$x>$`&&$x-2<$1}{say$i+0

Gotta love those arcane regex semantics.

Explanation:

/-(.+) (.):/

First, we get the minimum, maximum, and character to match into the $`, $1, and $2 variables, respectively ($` is the "everything before the match" string, which is our first number).

{$x=()=/$2/g}

Then we see how many $2 there are in the string and store that number in $x. Note that we'll overcount by one, since we're also counting the instance before the colon. The =()= is creatively called the "Saturn" operator and is one of the Perl "secret" operators. We wrap this in a block so that the regex variables (which are block-local for some reason) don't overwrite our $1 and $2 from the first match.

$i++if$x>$`&&$x-2<$1

Increment the variable $i if the number of matches (correcting for our earlier overcount) is between the min and the max. It's times like these that make me really wish Perl had Python's transitive comparison operators.

}{say$i+0

Another secret operator to take us to the end of the script, then we print the number of correct lines. We throw +0 at the end because $i starts out undefined, and we need to print zero if there were no matches at all.

You can learn about the secret operators I used here, and several others, on the relevant CPAN page.

\$\endgroup\$
2
\$\begingroup\$

Java (JDK), 281 271 249 244 bytes

int c,n,i,j;for(String[]x=s[i=n=c=0].split("\n"),a,r;i<x.length;n+=Integer.parseInt(r[0])>c|Integer.parseInt(r[1])<c?0:1)for(r=(a=x[i++].split(" "))[j=c=0].split("-");j<a[2].length();)if(a[1].charAt(0)==a[2].charAt(j++))c++;System.out.print(n);

Try it online!

Edit: Thanks @ceilingcat for -11 bytes.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ LOL, you are so smart! You put out of the box ideas and exemplify them in your code! I like how you initialize your variables in arrays. \$\endgroup\$ Dec 8 '20 at 21:57
  • \$\begingroup\$ 236 bytes \$\endgroup\$
    – ceilingcat
    Jan 7 at 6:38
2
\$\begingroup\$

C (gcc), 130 128 127 bytes

-1 byte thanks to ceilingat

m,M,l,d,t;f(S,n,c)char**S,c;{for(t=n;n--;t-=d<m|d>M,S++)for(d=!sscanf(*S,"%d-%d %c: %n",&m,&M,&c,&l);l[*S];)d+=l++[*S]==c;n=t;}

Try it online!

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

Javascript, 106 chars

s=>eval(s.replace(/(.+)-(.+)(.): (.+)/g,(m,l,r,c,p)=>"+"+eval(`p.replace(/${c}/g,x=>--l*--r),l>=0&r>=0`)))

Test:

f=s=>eval(s.replace(/(.+)-(.+)(.): (.+)/g,(m,l,r,c,p)=>"+"+eval(`p.replace(/${c}/g,x=>--l*--r),l>=0&r>=0`)))

console.log(f(`1-5 c: abcdefg
5-12 j: abcdefg
1-5 z: zzzzzzzz
3-3 h: hahaha
4-20 e: egejeqwee`))

console.log(f(`3-8 j: ksjjtvnjbjppjjjl
6-10 s: sszlkrsssss
1-4 z: znzfpz
7-11 m: dfkcbxmxmnmmtvmtdn
6-9 h: hlhhkhhhq
8-9 p: ppppppppvp
6-9 c: cccgccccmcch
5-8 g: bgkggjgtvggn
3-4 x: xxhk
10-12 h: hhhhhhhhhlhhhhdnh
5-6 j: zjjsjn
9-14 s: ksclwttsmpjtds
6-7 l: gtdcblql
1-4 d: dkndjkcd
1-6 k: kkkkkckmc
10-12 x: xxxxxxxxxrxl
2-5 v: fxdjtv
3-5 q: pjmkqdmqnzqppr
5-8 t: xtgtgtcht
6-11 j: nftjzjmfljqjrc
6-9 g: rskgggmgmwjggggvgb
11-12 v: vkvdmvdvvvdv
15-16 z: zzzzzzzzzzzzzzgxz
13-17 v: vvvvvvvvvvvvvvvvnv
9-14 j: clrfkrwhjtvzvqqj
1-12 l: lljljznslllffhblz
4-7 c: cccgcxclc
6-11 z: tbxqrzzbwbr
18-19 m: jmbmmqmshmczlphfgmf
1-5 s: ssvsq
4-5 k: kkkkw
4-7 f: fcqffxff
3-8 f: fffzthzf
9-12 v: vvvvvvvvvvvmvv
14-17 f: ftflcfffjtjrvfkffmvf
4-7 n: pnmnxnqqjp
6-7 r: rrrkrmr
4-13 k: drckdzxrsmzrkqckn
5-14 t: dttttttttttttt
13-14 v: vvvvfvvvvvvvvvv
7-12 j: jrljwnctjqjjxj
6-7 v: wchfzvm
1-8 d: dddddddzd
10-12 g: gvvzrglgrgggggggggg`))

\$\endgroup\$
1
\$\begingroup\$

SmileBASIC 4, 198 characters

@0:S=INSTR(I$[0]," ")C=I$[0][S+1]N=S+3
REPEAT N=INSTR(N+1,I$[0],C)INC M,N>0UNTIL N<0
H=INSTR(I$[0],"-")
U=VAL(LEFT$(I$[0],H))
V=VAL(MID$(I$[0],H+1,S-H-1))
INC O,M>=U&&M<=V:IF LEN(I$)GOTO@0'newline char here
?O

Receives input through I$[]. Simply parses the min-max char information and searches through each string of the string array, doing a tally and summing up the number of true hits.


Single-line (ish) version (same number of characters):

@0:S=INSTR(I$[0]," ")C=I$[0][S+1]N=S+3REPEAT N=INSTR(N+1,I$[0],C)INC M,N>0UNTIL N<0H=INSTR(I$[0],"-")U=VAL(LEFT$(I$[0],H))V=VAL(MID$(I$[0],H+1,S-H-1))INC O,M>=U&&M<=V:IF LEN(I$)GOTO@0
?O
\$\endgroup\$
0
\$\begingroup\$

Japt -x, 20 bytes

Takes input as an array of lines.

m¸£OvXÎr-'õ)øXÌèXÅÎÎ

Try it

\$\endgroup\$
1
  • 2
    \$\begingroup\$ How many more bytes for taking input as a string with newlines, as per the challenge? \$\endgroup\$
    – Sixtyfive
    Dec 6 '20 at 14:12

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.