You're a professional hacker and your boss has just ordered you to help his favorite candidate to win the election. Your task is to alter the voting machines data to boost your candidate's results.

Voting machines store voting results as two integers : the number of votes for your candidate (v1) and the number of votes for his opponent (v2).

After weeks of research, you have found a security hole in the system and you can increase the value of v1 by an integer x, and decrease the value of v2 by the same x. But there is a constraint, you have to keep the security hash code constant:

  • security hash code : (v1 + v2*2) modulo 7

Also, the value for x must be minimal so your changes can go unnoticed.

Your program should accept as input v1 and v2 ; it should output the optimal value for x so v1>v2.

There are some cases for which you cannot hack the results ; you don't have to handle them (this might lead to problems with your boss, but that's another story).

Test cases

100,123 --> 14
47,23 --> 0
40,80 --> 21
62,62 --> 7
1134,2145 --> 511
share|improve this question
3  
This feels to meet too much like a math problem you have to solve before golfing the expression. I think it would be better to say that x must be a multiple of 7 than to talk about the hash. – xnor 6 hours ago
2  
@SuperChafouin Though I complain about challenges like this, I don't believe they are considered off-topic, so the close vote might not go through. – xnor 6 hours ago
5  
We have questions that ask to find the product of two numbers that go unclosed. You'll be fine. – Pavel 6 hours ago
8  
In my opinion, there's no need to specify the boss's gender in this problem, nor the gender of your candidate. Defaulting to a boss/candidate being male is a symptom of our unconscious gender biases, which really do a lot of harm in STEM fields. – Greg Martin 5 hours ago
3  
I've flagged this question for moderator attention. Please, do not ask users on PPCG to write code that will be used to rig an election. – Stewie Griffin 2 hours ago

10 Answers 10

Python 2, 30 bytes

lambda a,b:max((b-a)/14*7+7,0)
share|improve this answer
    
After fixing the bug we basically have the same answer =/ – orlp 6 hours ago
2  
@orlp Yeah, I think this is just the way to write the expression. Unless a recursive solution is shorter, which I doubt. – xnor 6 hours ago
    
@xnor I don't know which one to ask, so I'll ask both of you, can you please explain to me how you thought of this? y<x?0:(y-x)/2-(y-x)/2%7+7;, I thought that I should take the difference split it in half, and then find the nearest multiple of 7. How did you arrive to this? – Wade Tyler 2 hours ago
1  
@WadeTyler We're looking the the smallest multiple of 7 that is strictly greater than half the difference. To find that from (b-a)/2, we do /7*7 to round downs to the nearest multiple of 7, and then +7 to go up to the next up. That is, unless the we would get a negative number, in which case we're winning anyway can just do 0. Taking the max with 0 achieves this. Some of it was also just tweaking the expression and running it on the test cases to see what works. – xnor 2 hours ago
1  
@WadeTyler The /7*7 is a kind of expression that shows up often enough in golfing that I think of it as an idiom. The idea is the n/7 takes the floor of n/7, i.e. finds how many whole multiples of 7 fit within n. Then, multiplying by 7 brings it to that number multiple of 7. – xnor 2 hours ago

Python 2, 30 bytes

lambda u,t:max(0,(t-u)/14*7+7)

u is our votes, t is their votes.

share|improve this answer
    
I didn't know *- was valid python. Huh. – Pavel 6 hours ago
    
@Pavel That's just * followed by -7. – orlp 6 hours ago
    
Well, I did a stupid. – Pavel 6 hours ago
    
This doesn't work for 62,62 --> 7. – xnor 6 hours ago
2  
Couldn't (t-u)/14*7 be just (t-u)/2? – Conor O'Brien 6 hours ago

Mathematica, 22 bytes

0//.x_/;2x<=#2-#:>x+7&

Pure function with arguments # and #2. Starting with 0, repeatedly replace any x such that #+x <=#2-x with x+7. Hits maximum recursion depth if the discrepancy is more than 7*2^7 = 458752.

share|improve this answer
1  
Can you add an explanation for all this? – Pavel 6 hours ago

J, 15 bytes

0>.7+7*14<.@%~-

Kinda interesting, I was working on a problem and I thought I had a solution but as it turns out I was wrong. Oh well. Try it online! Here's the result:

   f =: 0>.7+7*14<.@%~-
   tests =: 123 100 ; 23 47 ; 80 40 ; 62 62 ; 2145 1134
   (,. f/ each) tests
┌─────────┬───┐
│123 100  │14 │
├─────────┼───┤
│23 47    │0  │
├─────────┼───┤
│80 40    │21 │
├─────────┼───┤
│62 62    │7  │
├─────────┼───┤
│2145 1134│511│
└─────────┴───┘
share|improve this answer
    
In the future, please use TIO.run/nexus – Pavel 4 hours ago

Actually, 13 bytes

7;;τ((-\*+0kM

Try it online!

Uses the same max((b-a)/14*7+7,0) formula that xnor and orlp use.

Explanation:

7;;τ((-\*+0kM
7;;            3 copies of 7
   τ           double one of them
    ((-        bring the inputs back to the top, take their difference
       \*+     integer divide by 14, multiply by 7, add 7
          0kM  maximum of that and 0
share|improve this answer
    
Actually, this is a great answer – TrojanByAccident 3 hours ago

CJam, 13 bytes

q~\-D/7*7+0e>

Blatantly stole orlp and xnor's methods.

Input is the two numbers separated by a space: 100 123

Explanation:

q~\-D/7*7+0e>
q~\           e# Execute the input to push the two numbers onto the stack. Swap them.
   -          e# Subtract the two numbers on the stack.
    D/        e# Divide by 0xD (14)
      7*      e# Multiply by 7
        7+    e# Add 7
          0e> e# Push 0 and take the max of the two values
share|improve this answer

Groovy, 37 bytes

{x,y->[(y-x).intdiv(14)*7+7,0].max()}

This is an unnamed closure. Nothing special here, thanks to xnor and orlp for the formula.

Try it here!

share|improve this answer

Jelly, 9 bytes

IH:7‘×7»0

Try it online!

How it works

IH:7‘×7»0  Main link. Argument: [v1, v2]

I          Increments; compute [v2 - v1].
 H         Halve the result.
  :7       Perform integer division by 7.
    ‘      Increment the quotient.
     ×7    Multiply the result by 7.
       »0  Take the maximum of the product and 0.
share|improve this answer

Julia 0.5, 24 bytes

v\w=max((w-v)÷14*7+7,0)

Try it online!

share|improve this answer

Haskell, 30 bytes

a#b=head[c|c<-[0,7..],a+c>b-c]

An infix operator taking the number of votes of your preferred candidate first. Creates a list of each valid number (a multiple of 7) and filters so that your preferred candidate would have most votes. Can be used as v1 # v2, which gives x.

share|improve this answer
    
Finding the first value that meets a condition is a good use for until: a#b=until(\c->a+c>b-c)(+7)0, or better a%b=until(>(b-a)/2)(+7)0. Though an arithmetic formula is still likely shorter. – xnor 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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