12
\$\begingroup\$

Backstory

You own a tiny jewellery shop in the suburbs of the city. The suburbs are too much overpopulated, so your shop has a thickness of only one character to fit in the busy streets.

Recently, there has been lots of cases of robbery on the neighborhood, and you fear that robbers will also come to get your bounty.

Therefore, you installed surveillance cameras to take care of the shop. But there is one big problem: The cameras do not beep or alarm.


You decide to program the security alarm by yourself. This will complete the contraptions and (hopefully) make your little shop safe and secure.

Task

  • Your surveillance cameras will map your shop like this:

    WI  J E W
    

    This is your input, which you can take from STDIN or from the command-line arguments. Each of the letters have different meanings.

    • W represents a wall. Robbers and intruders cannot pass through them.
    • E means employees. These are the beings that the cameras recognise. If they see an intruder, they will immediately set off the alarm before the robbers can do anything. (They can see an intruder if there is no wall, jewel, intruder or other employee in between) They also tend to stand still.
    • J means jewels. These are the stuff the robbers and intruders are looking for.
    • I means intruder. They are most likely robbers. Their goal is to steal (at least one of) the jewels of the shop.
    • Of course, you can create your own legend of the map for the sake of saving code bytes.
  • Based on the input, you need to write a program (or function) that does the following:

    • If an intruder can freely put their hands on a jewel OR an employee can see an intruder:
      • Print a truthy value. ("1", "True", "Alert")
      • To be completely alert, print the ASCII bell character. (It has an ASCII code of 7. When printed, it plays a ting sound on lots of computers and implementations)
        • In most implementations, there cannot be a ting sound, so be carefull!
        • In some cases where the bell absolutely cannot be printed, print an exclamation ('!') instead of it. (Printing exclamation marks may be harder to implement in some languages)
    • Else:
      • Print a falsey value. ("0", "False", "Quiet")
  • The shop does not wrap around.

  • Welp, your computer runs slow when you run too much bytes. Try to program your code as short as possible. (code-golf)

Example tests

STDIN:        EXAMPLE STDOUT:
WI  J E W     1(BEL)

WIWWJWE W     0

E I J I E     1(BEL)

I   W J E     0

I E W E E     1(BEL)

IIIIEIIII     1(BEL)

JJJJEJJWI     0

Note: "(BEL)" refers to the bell character, not the string.

Good luck!

\$\endgroup\$
13
  • 1
    \$\begingroup\$ In the third example why can't the employees see the intruders and why can't the intruders grab the jewels? \$\endgroup\$
    – user253751
    Sep 15, 2020 at 18:26
  • \$\begingroup\$ Mistakes are now edited. Thanks for pointing out! \$\endgroup\$
    – SunnyMoon
    Sep 15, 2020 at 18:29
  • 1
    \$\begingroup\$ "Everybody be cool, this is a robbery!" \$\endgroup\$
    – Arnauld
    Sep 15, 2020 at 19:17
  • 2
    \$\begingroup\$ You may want to rephrase, so. Currently it reads as though we must output a truthy value or, optionally, BEL/!. \$\endgroup\$
    – Shaggy
    Sep 15, 2020 at 19:19
  • 3
    \$\begingroup\$ Is it acceptable to outout two (or more) BEL characters? \$\endgroup\$ Sep 15, 2020 at 20:23

12 Answers 12

9
\$\begingroup\$

Jelly,  10  8 bytes

ḟ⁶OIA7fỌ

A full program which prints an empty string (falsey) if all is well or a bell character if not (a bell character is also truthy).

Input:

    Wall W
Employee X
   Jewel J
Intruder Q

Try it online!

Or see the test-suite (The footer first translates the characters from the example ones and calls the Link for each line).

How?

ḟ⁶OIA7fỌ - Main Link: list of characters in "W QJX"
 ⁶       - a space character
ḟ        - filter-discard (remove any spaces)
  O      - to ordinals (e.g. "WXQJ" -> [87,88,81,74])
   I     - incremental differences (e.g. [87,88,81,74] -> [1,-7,-7])
           ...possible values are: -14 -13 -7 -6 -1 0 1 6 7 13 14
              -7 and 7 indicate an intruder (Q) is next to a jewel (J) or employee (X)
    A    - absolute values
     7   - seven
      f  - filter keep ([7] if any of the values are 7 else []) 
       Ọ - cast to characters (bell character in a list or an empty list)
         - implicit print
           ...single-element lists print their element
              while empty lists print an empty string
\$\endgroup\$
9
\$\begingroup\$

Brachylog, 17 12 10 bytes

-5 bytes by not distinguishing between Employees and Jewels

-2 bytes using addition, not multiplication, so I get 7 for \a for free

Empty tiles are -, intruders are also humans, jewels are still Jewels, the shop employees are e, and walls > are closing in. In Brachylog a predicate succeeding is the truthy value. So it's either a value as an output (BEL) or the unification failed, represented as false..

ạ%₉ᵐs+7g~ạ

Try it online!

This is all done so the bytes modulo 9 ạ%₉ᵐ map (empty) => 0, I => 5, J => 2, E => 2, W => 8. With this, we can sum every subset of consecutive elements s+ and check if one of them is 7 (I J, i.e. 5+0+0+2 or E I, i.e. 2+0+0+5). Because empty tiles map to 0, they don't change the value, and neither does the order. Also 7 cannot be made by other elements. If one subset matches, return convert 7 to a byte ~gạ, which is \a.

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 7 bytes

áÇ¥Ä7Ãç

Port of @JonathanAllan's Jelly answer, so make sure to upvote him as well!

Just like his answer, I use W=wall; J=jewel; X=employee; Q=intruder.
Outputs one or multiple BEL characters in a list as truthy value or an empty list as falsey value.

Try it online or verify all test cases.

Explanation:

á        # Only keep the letters of the (implicit) input-string (removes spaces)
 Ç       # Convert each character to its codepoint integer
  ¥      # Get the forward difference between each codepoint pair
         #  (one of: [-14,-13,-7,-6,-1,0,1,6,7,13,14])
   Ä     # Take the absolute value of each difference
    7Ã   # Only keep all 7s in the list
      ç  # And convert those 7s (if any) to an ASCII character with this codepoint
         # (after which the resulting list is output implicitly as result)
\$\endgroup\$
5
\$\begingroup\$

Python 3, 52 bytes

Uses # instead of for empty spaces and instead of W for walls. Output is an empty string as a falsey value and the bell character for truthy inputs.

lambda s:any({*'I#'}<{*g,'#'}for g in s.split())*''

Try it online!

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

JavaScript (ES6),  33  28 bytes

Saved 5 bytes thanks to @DomHastings!

Expects e for a jewel, and the characters defined in the challenge for the other items.

The unprintable BEL is escaped below.

s=>/E *I|I *E/i.test(s)&&'\7'

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ I cannot see any truthy value before the bell characters. By the way you did it quite quick! \$\endgroup\$
    – SunnyMoon
    Sep 15, 2020 at 18:38
  • 5
    \$\begingroup\$ @SunnyMoon BEL itself is a truthy output in JS. \$\endgroup\$
    – Arnauld
    Sep 15, 2020 at 18:44
  • 3
    \$\begingroup\$ If you use e for the jewels you could add i to the end to save some bytes! \$\endgroup\$ Sep 15, 2020 at 20:25
  • \$\begingroup\$ @DomHastings Very nice trick! \$\endgroup\$
    – Arnauld
    Sep 15, 2020 at 21:10
4
\$\begingroup\$

AWK, 47 \$\cdots\$ 27 26 bytes

Saved 2 3 7 8 bytes thanks to Dominic van Essen!!!
Has unprintable bell character in quotes.

$0=/[EJ] *I|I *[EJ]/?"":0

Try it online! (With printable bell)

If there's possibly danger at hand outputs bell character (truthy) or outputs 0 (falsey) otherwise.

\$\endgroup\$
13
  • \$\begingroup\$ This does calculate the security, but it fails to ring the terminal bell. \$\endgroup\$
    – SunnyMoon
    Sep 15, 2020 at 20:01
  • \$\begingroup\$ @SunnyMoon Fixed - got it now! Was confusing bell with truthy/falsey output. \$\endgroup\$
    – Noodle9
    Sep 15, 2020 at 22:59
  • \$\begingroup\$ "\7" is truthy in awk for an easy 1 byte save, and you can get another by switching 0 for "0"... \$\endgroup\$ Sep 16, 2020 at 8:41
  • \$\begingroup\$ ...and "" (the empty string) is falsy in awk to cut it down even more... \$\endgroup\$ Sep 16, 2020 at 8:47
  • \$\begingroup\$ ...(or, with more ruthless cutting-down: 29 bytes, or 28 if you don't mind the trailing newline)... \$\endgroup\$ Sep 16, 2020 at 9:19
3
\$\begingroup\$

perl -pl, 26 bytes

$_=/I *[EJ]|[EJ] *I/?"^G":0

Try it online!

Since it's hard to enter unprintable characters, the bell character is here (and in TIO), represented by the two character combo ^G. In the real program, this is the character with ASCII code 7 (so, I'm counting it as 1 character).

How does it work?

All the described cases boil down to an intruder next to either some jewels or an employee. So we use a regexp to detect this case.

perl -pl, 15 bytes

s/E *I|I *E/^G/i

Try it online!

Here, "truthy" is taken to "contains a BEL character", and "falsey" as "does not contain a BEL character". And it uses @Dom Hastings suggestion of using e as the symbol for jewels.

perl -F/[EJ]\s*I|I\s*[EJ]/ -pl, 13 bytes

$_=@F>1?"^G":0

Try it online!

We can offload part of the work to a command line switch, and reduce it to 13 bytes.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice one! There's definite opportunity to a use -F as always... But more practically,you could use x to return either 0 or 1 BELs and like I mentioned to Arnauld on their answer, if you use e for the jewels you could make the search case insensitive to combine [EJ] into E. I did play a bit with the set of chars '7GWgw to try and come up with a way of being able to generate the BEL using &= but couldn't get something that worked... Might be something there though! \$\endgroup\$ Sep 15, 2020 at 21:30
  • \$\begingroup\$ @DomHastings Nice. Taking your suggestion, and really stretching the meaning of "truthy" and "falsy", I can do it in 15 bytes. \$\endgroup\$
    – Abigail
    Sep 15, 2020 at 21:47
  • \$\begingroup\$ Ah, your third solution is why I asked about repetition, you can get that down to 11 bytes: Try it online! \$\endgroup\$ Sep 16, 2020 at 16:59
3
\$\begingroup\$

Pip, 26 21 bytes

3NST(_-BMPaRMs)?o.'!i

Try it online!

-5 bytes after Dominic Van Essen's input changes.

Takes inputs as:

8 → Wall
4 → intruder
7 → Jewel
1 → Employee

Takes the differences, converts to string, checks if there's 3 in the string representation.

\$\endgroup\$
2
  • \$\begingroup\$ 25 bytes by taking Jewel as 7 and Intruder as 4. \$\endgroup\$ Sep 16, 2020 at 12:28
  • \$\begingroup\$ That change reduces 5 bytes. nice. \$\endgroup\$
    – Razetime
    Sep 16, 2020 at 12:30
2
\$\begingroup\$

Pyth, 13 bytes

*\lf&/TN@GTc

Try it online!

Legend

  • j - jewel
  • e - employees
  • " - intruder
  • - wall
  • # - empty

Explanation

*\!lf&/TN@GTc
    f          # filter
            c  # input split on whitespace chars
               # with lambda T:
         @GT   #     some lowercase alphabet in T
     &/TN      #     and '"' in T
*\!l           # output '!' repeated length of results of filter times  
\$\endgroup\$
1
  • \$\begingroup\$ Does printing a single exclamation point take more bytes? \$\endgroup\$
    – Razetime
    Sep 16, 2020 at 1:42
2
\$\begingroup\$

R, 74 bytes

function(x)`if`(grepl('IE|EI|IJ|JI',gsub(' ', '',x)),intToUtf8(c(49,7)),0)

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Well done. I like that this isn't just a 'clone' of the other awk/perl regex approaches, even if it's a bit longer. You can shorten it a bit by removing the space, and using "\a" directly for 'BEL': 60 bytes \$\endgroup\$ Sep 16, 2020 at 8:24
  • \$\begingroup\$ Very nice improvement thank you! \$\endgroup\$
    – Cong Chen
    Sep 16, 2020 at 9:24
2
\$\begingroup\$

C (gcc), 97 bytes

i;j;r;t;f(char*s){for(i=j=r=0;t=*s++;t==87?i=j=0:0)i|=t==73,j|=t==74|t==69,r|=i&j;r&&putchar(7);}

Try it online (with exclamation point instead of bell since TIO can't handle it)

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

Charcoal, 18 bytes

×!⊙⪪EIIEIJJI²№⁻θ ι

Try it online! Link is to verbose version of code. Outputs ! since BEL isn't in Charcoal's code page. Explanation:

    EIIEIJJI        Literal string `EIIEIJJI`
   ⪪        ²       Split into 2-character substrings
  ⊙                 Any substring satisfies
             №      (non-zero) Count of
                 ι  Current substring in
               θ    Input string
              ⁻     With spaces deleted
×!                  `!` if the above is true
                    Implicitly print
\$\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.