Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public static int[] solution(int[] pegs) {
- /*source: http://nicholas.rinard.us/2017/03/foo bar-gearing-up-for-destruction.html
- if that link doesn't work use this one: https://web.archive.org/web/20220415012306/http://nicholas.rinard.us/2017/03/foo bar-gearing-up-for-destruction.html (remove the spaces between foo bar in the urls. i had to put spaces or pastebin blocks it)*/
- // finds the radius of gear 1
- int numerator = pegs[0];
- int negative = -1;
- for(int peg: pegs) {
- numerator+= 2 * peg * negative;
- negative *= -1;
- }
- numerator+= pegs[pegs.length-1] * negative;
- numerator*= 2;
- int denominator = (pegs.length%2==0) ? 3 : 1;
- // simplifies the fraction
- if(numerator%denominator==0) {
- numerator/= denominator;
- denominator = 1;
- }
- // checks to see if the fraction is spatially sound
- float limit = ((float)numerator) / ((float)denominator);
- for(int i = 0; i < pegs.length - 2; i++) {
- int space = pegs[i+1] - pegs[i];
- if(limit < 0 || limit > (space-1)) return new int[] {-1, -1};
- limit = space - limit;
- }
- return new int[] {numerator, denominator};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement