# Hack the elections

You're a professional hacker and your boss has just ordered you to help a candidate win an upcoming election. Your task is to alter the voting machines data to boost the 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 their 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

• Comments are not for extended discussion; this conversation has been moved to chat. – Dennis Jan 11 '17 at 17:00
• Also, to the close voters: This is perfectly on-topic. If you don't like it, you can downvote it. – Rɪᴋᴇʀ Jan 11 '17 at 17:51
• What a secure hash function! – Cruncher Jan 11 '17 at 21:25
• Can you assume the inputs are followed by .0 (Like 100.0 123.0)? – Esolanging Fruit Jan 18 '17 at 17:55

# Python 2, 30 bytes

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


u is our votes, t is their votes.

• Couldn't (t-u)/14*7 be just (t-u)/2? – Conor O'Brien Jan 11 '17 at 4:32
• Oh, wait, nevermind, Py2 does integer division – Conor O'Brien Jan 11 '17 at 4:37
• @ConorO'Brien Nope. Consider t-u == 16. Then 16/14*7 = 7, but 16/2 = 8. Also, you aren't running as Python 2. – orlp Jan 11 '17 at 4:37
• @orlp 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 Jan 11 '17 at 7:53
• the same solution is above – username.ak Jan 13 '17 at 11:24

## Python 2, 30 bytes

lambda a,b:max((b-a)/14*7+7,0)

• @orlp Yeah, I think this is just the way to write the expression. Unless a recursive solution is shorter, which I doubt. – xnor Jan 11 '17 at 4:06
• @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 Jan 11 '17 at 7:53
• @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 Jan 11 '17 at 8:08
• @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 Jan 11 '17 at 8:16
• @JackAmmo That example gives -2/7*7, and since Python floor-division rounds towards negative infinity, 2/7 is -1, so 7*-7+1 is 0. So, both sides give 0, which works out fine. – xnor Jan 14 '17 at 4:35

## Mathematica, 22 bytes

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


Pure function with arguments # and #2. Hits maximum recursion depth if the discrepancy is more than 7*2^16 = 458752.

## Explanation

0                       Starting with 0,
//.                    repeatedly apply the following rule until there is no change:
x_                    if you see an expression x
/;                    such that
2x<=#2-#            2x <= #2-# (equivalently, #+x <= #2-x)
:>        then replace it with
x+7       x+7 (hash is preserved only by multiples of 7)
&  End the function definition

• Can you add an explanation for all this? – Pavel Jan 11 '17 at 4:26
• @Pavel Maybe your comment has continued getting upvotes because my explanation was unclear? – ngenisis Jan 11 '17 at 20:52
• I thought it was fine, but then again I also know Mathematica. – Pavel Jan 11 '17 at 21:46
• @Pavel Well it's better now :) – ngenisis Jan 11 '17 at 21:48

# 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.


# 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

• Actually, this is a great answer – TrojanByAccident Jan 11 '17 at 6:48
• I feel like the name of this language was intentional to make the submission titles sound like punchlines: "Guys, Actually, this is 13 bytes! Come on!" – Patrick Roberts Jan 12 '17 at 10:31
• @PatrickRoberts Actually, that's correct. – Mego Jan 12 '17 at 10:39

## Groovy, 41 37 bytes

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


This is an unnamed closure. Thanks to xnor and orlp for the formula and James holderness for pointing out a bug.

The previous solution used intdiv() for integer division but it behaves differently from // used in python.

Try it here!

# Japt, 14 bytes

V-U /2+7 f7 w0


Run it here!

Thank you ETHproductions for shaving off 3 bytes!

• Very nice. f accepts an argument and floors to a multiple of that number, so I think you can so V-U /2+7 f7 w0 to save three bytes. – ETHproductions Jan 11 '17 at 18:59

# 05AB1E, 9 bytes

-14÷>7*0M


Try it online!

Explanation

-          # push difference of inputs
14÷       # integer divide by 14
>      # increment
7*    # times 7
0   # push 0
M  # take max


Or a corresponding function with same byte-count operating on a number-pair

Î¥14÷>7*M


Try it online!

# Dyalog APL, 14 bytes

Takes v1 as right argument and v2 as left argument.

0⌈7×1+(⌊14÷⍨-)


0 ⌈ the maximum of zero and

7 × seven times

1 + (...) one plus...

⌊ the floor of

14 ÷⍨ a fourteenth of

- the difference (between the arguments)

TryAPL online!

# Befunge, 19 bytes

777+:&&\-+\/*:0*.@


Try it online!

This relies on a slightly different formula to that used by orlp and xnor, since the Befunge reference interpreter has different rounding rules to Python. Befunge also doesn't have the luxury of a max operation.

The basic calculation looks like this:

x = (v2 - v1 + 14)/14*7
x = x * (x > 0)


Examining the code in more detail:

7                     Push 7                                      
77+:                 Push 14 twice.                              [7,14,14]
&&               Read v1 and v2 from stdin.                  [7,14,14,v1,v2]
\-             Swap the values and subtract.               [7,14,14,v2-v1]
+            Add the 14 that was pushed earlier.         [7,14,14+v2-v1]
\/          Swap the second 14 to the top and divide.   [7,(14+v2-v1)/14]
*         Multiply by the 7 that was pushed earlier.  [7*(14+v2-v1)/14 => x]
:        Make a copy of the result                   [x,x]
0      Test if it's greater than 0.                [x,x>0]
*     Multiply this with the original result.     [x*(x>0)]
.@   Output and exit.


# Go, 36 bytes

func(a,b int)int{return(b-a)/14*7+7}

Try it online!

# JavaScript (ES6), 31 bytes

(a,b,c=(b-a)/14|0)=>c>0?c*7+7:0


f=(a,b,c=(b-a)/14|0)=>c>0?c*7+7:0
document.write(f(1134,2145))

# Java 8, 31 bytes

(a,b)->b<a?0:(a=(b-a)/2)+7-a%7;

This is a lambda expression assignable to IntBinaryOperator.

java rounds down for division with positive integers, so +7-a%7 is used to bump up the value to the next multiple of 7.

• a->b->(b=(b-a)/14*7+7)>0?b:0 is 3 bytes shorter, but I kinda like your approach more, so +1 from me. Almost every answer given already uses max((b-a)/14*7+7,0).. – Kevin Cruijssen Oct 11 '17 at 11:26
• i prefer using lambdas that return the result directly. and yeahb everyone did the formula a bit shorter but this was how i reasoned about the answer before checking everyone elses – Jack Ammo Oct 17 '17 at 13:27
• a->b->(b=(b-a)/14*7+7)>0?b:0 does return the result directly as well: Try it here. Or do you mean you prefer single-method lambdas above currying lambdas; (a,b)-> preference over a->b->, even though it's longer? – Kevin Cruijssen Oct 17 '17 at 13:47
• single method over currying, but thats just a personal preference – Jack Ammo Oct 17 '17 at 14:15

## Ruby, 26 27 bytes

->a,b{[(b-a)/14*7+7,0].max}


Basically the same as xnor's and orlp's Python solution, with a twist (no need to add 7, because of negative modulo, saves 1 byte in ruby, don't know about python)

No twist, the twist was just a bad case of cognitive dissonance. Forget it. Really. :-)

# Scala, 31 bytes

(a,b)=>Math.max((b-a)/14*7+7,0)


The ternary version is 2 bytes longer

# Noodel, 16 bytes

⁻÷14ɲL×7⁺7ḋɲl⁺÷2


Pulled equation from xor and orlp answers, but since Noodel does not have a max capability had to work around that.

Try it:)

### How it works

⁻÷14ɲL×7⁺7       # The equation...
⁻                # v2 - v1
÷14             # Pops off the difference, then pushes on the (v2 - v1)/14
ɲL           # Applies lowercase which for numbers is the floor function.
×7         # Multiplies that by seven.
⁺7       # Then increments it by seven.

ḋɲl⁺÷2 # To relate with the other answers, this takes the max between the value and zero.
ḋ      # Duplicates what is on the top of the stack (which is the value just calculated).
ɲl    # Pops off the number and pushes on the magnitude (abs value).
⁺   # Add the abs to itself producing zero if the number came out negative (which means we are already winning).
÷2 # Divides the result by two, which will either be zero or the correct offset.


# Pyth, 16 bytes

MtS+0[+7*7/-HG14


Try it here!