3
\$\begingroup\$

Introduction

Little Jimmy is going trick or treating. He lives in an odd neighborhood: some houses give out candy, and some give out toothbrushes. Now, Jimmy does not want to get too many toothbrushes. He wants candy! You must help him get some.

Challenge

Each street in Jimmy's neighborhood has a number. Each house on the street will give out either candy or toothbrushes. However, Jimmy's parents know he gets very hyper if he eats too much candy. To prevent this, they are only letting him go down one street in the neighborhood. Given a list of streets, you must output the street where Jimmy will get the most candy and the fewest toothbrushes. This means that, as said in the comments, even if one street gives out 500 candies and 1 toothbrush, it is worse than one that gives out 1 candy and no toothbrush.

  • The list of streets will be 2D. It will never be empty. Each street will be a list of truthy or falsy values. It might be empty. Falsy values represent a house that gives out toothbrushes, truthy ones represent houses that give out candy.
  • Your program must output the index of the street with the most truthy values and the fewest falsy ones, or the street that gives the most candy and the fewest toothbrushes. If all the streets have the same amount of candy and toothbrushes, you may output -1. It does not matter if the index is 0-based or 1-based. It can be stored as a string. The integer can be in any base.

Example I/O

Note: In these lists, True represents a truthy value and False a falsy one. 0 can be replaced with 1 if your language uses 1-indexing.

  • Streets: [[True,True,True],[False,False],[False]]
    Output: 0
  • Streets: [[False,True,True],[False,False],[True],[False]]
    Output: 2
  • Streets: [[],[False,False,True],[False]]
    Output: 0
  • Streets: [[True,True,False],[False,True,True],[True,False,True]]
    Output: -1
  • Streets: [[True,True,True,True,True,False],[True]]
    Output: 1
  • Streets: [[False, True],[False,True,True]] Output:1

Rules

This is , so shortest code in bytes wins!

\$\endgroup\$
  • 2
    \$\begingroup\$ Must we output -1 if two streets are equal, or can we output one of the streets? \$\endgroup\$ – TFeld Oct 31 at 13:54
  • 11
    \$\begingroup\$ Wait, so getting 500 pieces of candy and a toothbrush is worse than getting only 1 piece of candy and no toothbrushes? \$\endgroup\$ – Veskah Oct 31 at 13:56
  • 7
    \$\begingroup\$ I'm not totally following how to determine the answer. Are we first minimizing falses and then maximizing trues? In which case I would suggest the test case [[False, True], [False, True, True]] \$\endgroup\$ – Business Cat Oct 31 at 14:27
  • \$\begingroup\$ @Veskah, he must really hate toothbrushes. (Or love dentists) \$\endgroup\$ – Kjetil S. Nov 3 at 3:19
3
\$\begingroup\$

Python 2, 58 bytes

lambda l:l.index(max(l,key=lambda s:(-s.count(0),sum(s))))

Try it online!

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

R, 52 bytes

function(l)order(lengths(l)-(m=sapply(l,sum)),-m)[1]

Try it online!

Minimizes the number of toothbrushes, then maximizes the pieces of candy, since this is apparently what OP is asking.

If we want to first maximize the amount of candy, then minimize the number of toothbrushes (seems more plausible…) it becomes

R, 51 bytes

function(l)order(m<--sapply(l,sum),lengths(l)+m)[1]

Try it online!

Computing the number of TRUEs in a list of vectors is easy with sapply(l,sum). Computing the number of FALSEs is much less golfy, so I use lengths minus the number of TRUEs instead.

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

Mathematica, 180 172 bytes

ToExpression[StringReplace[a,{"["->"{","]"->"}"," "->"=","True"->"-10^-9","False"->"1"}]];(If[Length[#]==1,#[[1]],-1]&@@Flatten[Position[#,Min[#]]]&/@{Total/@Streets})[[1]]

We assume the input is already stored as text in the variable a. If the reformatting code is neglected (input is already stored in the variable Streets) the length becomes 82 bytes.

My solution maps the toothbrushes (1) + candy(-1e-9) onto the real line and finds the minimum. It will only work for streets that have a maximum of 1,000,000,000 toothbrush houses as is (but it can be easily modified to suit the max number of toothbrush houses.)

Expanded code:

a := "Streets: [[True,True,False],[False,True,True],[True,False,True]]";
ToExpression[ 
 StringReplace[
  a, {"[" -> "{", "]" -> "}", " " -> "=", "True" -> "-10^-9", 
   "False" -> "1"}]]
b = (Total /@ Streets)
c = Flatten[Position[b, Min[b]]]
If[Length[c]==1,c[[1]],-1]
\$\endgroup\$
0
\$\begingroup\$

Jelly, 15 bytes

ċþØ.N1¦ZM-¹L’$?

Try it online!

A monadic link taking a list of lists of Jelly booleans (0s and 1s). Returns the 1-indexed maximum list using the specified criteria, or -1 if there is more than one that is equal. The last 6 bytes are spent on converting multiple values into -1; if this could be dropped the answer would be 9 bytes.

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

J, 19 bytes

0{[:/:(-.,&(+/)-)@>

Try it online!

  • 0 { first element of...
  • [: /: the grade up of... (indices with smallest item first)
  • (-.,&(+/)-) the list of pairs , created by summing each of &(+/): 1 minus an input item -. and the negative of an input item -
  • @> of each unboxed input item

This turns each input item into a pair:

<number of false, -1 * number of true>

When we sort this list J will automatically sort first by element 1, then by element 2. Lower are numbers come first.

\$\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.