Honeycomb Distance
Source: aizu

Problem C
Honeycomb Distance

A plane is tiled completely with regular hexagons. Each hexagon is called a cell. You can move in one step from a cell to one of its adjacent cells that share an edge. You want to know the minimum number of steps to move from the central cell to the specified cell.

Each cell is specified by a pair of integers. The central cell is (0, 0). One of the directions perpendicular to the edges of the regular hexagons is defined to be the rightward direction. For each cell (x, y), its right adjacent cell is (x + 1, y) and its upper-right adjacent cell is (x, y + 1). See the figure below.

Write a program that computes the minimum number of steps required to move from the cell (0, 0) to the cell (x, y) for given integers x and y.

Figure C-1: How to specify the cells
Input

The first line of the input contains only one positive integer n, which is the number of datasets. n does not exceed 100. Each of the following n lines contains one dataset.

Each dataset consists of two integers x and y, which satisfy −1000 ≤ x ≤ 1000 and −1000 ≤ y ≤ 1000. The destination cell is (x, y).

Output

For each dataset, output one line containing the minimum number of steps required to move from the cell (0, 0) to the destination cell.

Sample Input
7
0 0
0 1
1 0
2 1
2 -1
-3 2
-1 -3

Output for the Sample Input
0
1
1
3
2
3
4


C++ solution:
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

int main(void){
    bool is_a_lead = false;
    bool is_draw = true;
    queue<int> ans;
        int n;
        cin >> n;
        for(int i=0;i<n;i++){
            int a,b;
            cin >> a >> b;
            int count = 0;
            while(a < 0 && b > 0){
                a++;
                b--;
                count++;
            }
            while(a > 0 && b < 0){
                a--;
                b++;
                count++;
            }
            ans.push(count + abs(a) + abs(b));
        }


    while(!ans.empty()){
        cout << ans.front() << endl;
        ans.pop();
    }
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	for i := 0; i < n; i++ {
		scanner.Scan()
		line := scanner.Text()
		parts := strings.Fields(line)
		x, _ := strconv.Atoi(parts[0])
		y, _ := strconv.Atoi(parts[1])

		if x == 0 && y == 0 {
			fmt.Println(0)
		} else if x >= 0 && y >= 0 {
			fmt.Println(x + y)
		} else if x <= 0 && y <= 0 {
			fmt.Println(-x + -y)
		} else {
			absX := x
			if x < 0 {
				absX = -x
			}
			absY := y
			if y < 0 {
				absY = -y
			}
			if absX > absY {
				fmt.Println(absX)
			} else {
				fmt.Println(absY)
			}
		}
	}
}

Java solution:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            if (x == 0 && y == 0) {
                System.out.println(0);
            } else if (x >= 0 && y >= 0) {
                System.out.println(x + y);
            } else if (x <= 0 && y <= 0) {
                System.out.println(Math.abs(x) + Math.abs(y));
            } else {
                System.out.println(Math.max(Math.abs(x), Math.abs(y)));
            }
        }
        scanner.close();
    }
}

JavaScript solution:
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let lines = [];
rl.on('line', (line) => {
    lines.push(line);
}).on('close', () => {
    const n = parseInt(lines[0], 10);
    for (let i = 1; i <= n; i++) {
        const [x, y] = lines[i].split(' ').map(Number);
        if (x === 0 && y === 0) {
            console.log(0);
        } else if (x >= 0 && y >= 0) {
            console.log(x + y);
        } else if (x <= 0 && y <= 0) {
            console.log(Math.abs(x) + Math.abs(y));
        } else {
            console.log(Math.max(Math.abs(x), Math.abs(y)));
        }
    }
});

Python solution:
n=int(input())
for i in range(n):
    x,y=list(map(int,input().split()))
    if x==y==0:
        print(0)
    elif x>=0 and y>=0:
        print(x+y)
    elif x<=0 and y>=0:
        print(max(abs(x),abs(y)))
    elif x<=0 and y<=0:
        print(abs(x)+abs(y))
    else:
        print(max(abs(x),abs(y)))

Ruby solution:
gets.to_i.times{
    x, y = gets.chomp.split.map(&:to_i)
    if y >= 0     
        if x < -y
            puts -x
        elsif x.between?(-y, 0)
            puts y
        else
            puts x + y
        end
    else
        if x < 0
            puts -(x + y)
        elsif x.between?(0, -y)
            puts -y
        else
            puts x
        end
    end
}