A to B
Source: codechef
Difficulty: 2118

A to B
You are given two points$A = (x_1, y_1, z_1)$and$B = (x_2, y_2, z_2)$in the 3D coordinate system.
You would like to start at point$A$and reach point$B$.
To do this, you can move in any of the$6$cardinal directions — up, down, left, right, front or back — one step at a time.
Formally, in one step, you can choose exactly one coordinate and either increase or decrease it by$1$.
However, you are not allowed to move more than$K$steps
consecutively
in one direction.
Your task is to determine the minimum number of steps required to move from$A$to$B$.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
The first and only line of each test case contains$7$space-separated integers — the values$x_1, y_1, z_1, x_2, y_2, z_2, K$in order — denoting the coordinates of$A$, the coordinates of$B$, and the parameter$K$.
Output Format
For each test case, output on a new line a single integer: the answer to the problem.
Constraints$1 \leq T \leq 10^4$$1 \leq x_1, y_1, z_1, x_2, y_2, z_2, K \leq 5\cdot 10^8$Sample 1:
Explanation:
Test case$1$:
You want to move from$(1, 2, 3)$to$(6, 2, 3)$, while not moving more than$K = 1$time consecutively, meaning the same move can't be made twice in a row.
This requires$9$moves at minimum, here is one valid sequence:$(1, 2, 3) \to (2, 2, 3) \to (2, 3, 3) \to (3, 3, 3) \to (3, 3, 4) \to (4, 3, 4) \to (4, 2, 4) \to (5, 2, 4) \to (5, 2, 3) \to (6, 2, 3)$.
You can verify that the same move was never made twice in a row.
Test case$2$:
You want to move from$(1, 1, 1)$to$(2, 2, 2)$.
You can move$1$step in the positive$x$-direction,$1$step in the positive$y$-direction, and$1$step in the positive$z$-direction, resulting in a total of$3$moves.

Sample Input:
3
1 2 3 6 2 3 1
1 1 1 2 2 2 2
400 500 600 1000 1500 4000 3

Sample Output:
9
3
5000

C++ solution:
#include <bits/stdc++.h>
using namespace std;

#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define cwr(x) {cout << x << '\n'; return;}
#define int long long
#define nl '\n'

int T = 1;

void solve() {
    int x1, y1, z1, x2, y2, z2, k;
    cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> k;

    int max_diff = max({abs(x1 - x2), abs(y1 - y2), abs(z1 - z2)});
    int total_dist = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);

    int max_moves = ((max_diff + k - 1) / k) - 1;
    int moves = total_dist - max_diff;

    int diff = max_moves - moves;

    cwr(((diff < 0) ? total_dist : total_dist + diff + (diff & 1)));
}

signed main() {
    fastio;
    cin >> T;
    while (T--) solve();
    return 0;
}

Go solution:
package main

import (
    "fmt"
)

func main() {
    var t int
    fmt.Scan(&t)

    for t > 0 {
        t--

        var x1, y1, z1, x2, y2, z2, k int64
        fmt.Scan(&x1, &y1, &z1, &x2, &y2, &z2, &k)

        dx := abs(x2 - x1)
        dy := abs(y2 - y1)
        dz := abs(z2 - z1)

        maxDiff := max(dx, dy, dz)
        sumSmaller := dx + dy + dz - maxDiff
        requiredSteps := (maxDiff + k - 1) / k

        var result int64
        if sumSmaller >= requiredSteps-1 {
            result = dx + dy + dz
        } else {
            excess := requiredSteps - 1 - sumSmaller
            if excess > 0 && excess%2 == 1 {
                result = dx + dy + dz + excess + 1
            } else {
                result = dx + dy + dz + excess
            }
        }

        fmt.Println(result)
    }
}

func abs(x int64) int64 {
    if x < 0 {
        return -x
    }
    return x
}

func max(a, b, c int64) int64 {
    if a > b {
        if a > c {
            return a
        }
        return c
    }
    if b > c {
        return b
    }
    return c
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0) {
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int z1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            int z2 = sc.nextInt();
            int k = sc.nextInt();
            int d[] = {Math.abs (x1 - x2), Math.abs (y1 - y2), Math.abs (z1 - z2)};
            Arrays.sort(d);
            int gaps = (d[2] + k - 1) / k - 1;
            int extras = d[0] + d[1];
            if (extras >= gaps) System.out.println(d[0] + d[1] + d[2]);
            else System.out.println(d[2] + extras + (gaps - extras) + ((gaps - extras) % 2));
        }

    }
}

JavaScript solution:
/* -------------------------------------------------------- */
/*   ( The Authentic JS CodeBuff )
 ___ _                      _              _ 
 | _ ) |_  __ _ _ _ __ _ __| |_ __ ____ _ (_)
 | _ \ ' \/ _` | '_/ _` / _` \ V  V / _` || |
 |___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
                                        |__/ 
*/
/* ---------------------------------------------------------   */
/*    Youtube: https://youtube.com/@code-with-Bharadwaj        */
/*    Github : https://github.com/Manu577228                  */
/* ----------------------------------------------------------- */

// Template Starts here Fast IO!

const V8Flags = [
    '--max-old-space-size=4096', '--max-semi-space-size=16', '--stack-size=65536', '--reclaim-unused-memory',
    '--gc-interval=100', '--expose-gc', '--gc-global', '--optimize-for-speed',
    '--always-opt', '--max-inlined-source-size=600', '--max-inlined-bytecode-size=600', '--turbo-inline-js-wasm-calls',
    '--turbo', '--turbo-optimizer', '--force-optimization', '--no-lazy',
    '--no-lazy-inner-functions', '--optimize-for-size', '--initial-heap-size=64', '--max-heap-size=192',
    '--use-strict', '--predictable', '--single-threaded-gc', '--no-inline-new',
    '--opt', '--no-lazy-deserialization', '--no-browser-globals', '--turbo-inline-array-builtins',
    '--fast-math', '--fuzzing', '--minor-mc', '--incremental-marking',
    '--concurrent-recompilation', '--stress-compaction', '--experimental-flush-embedded-blob-icache', '--experimental-value-unavailable'
];

if (V8Flags.length > 0) {
        process.execArgv.push('--v8-options=' + V8Flags.join(' '));
}

const inputBuffer = Buffer.allocUnsafe(1e7);
let inputIndex = 0,
        inputLength = 0;
const outputBuffer = Buffer.allocUnsafe(1e7);
let outputIndex = 0;

const fs = require('fs');
inputLength = fs.readSync(process.stdin.fd, inputBuffer, 0, inputBuffer.length, null);

function readInt() {
        let num = 0,
                sign = 1;
        while (inputBuffer[inputIndex] < 48 || inputBuffer[inputIndex] > 57) {
                if (inputBuffer[inputIndex] === 45) sign = -1;
                inputIndex++;
        }
        while (inputIndex < inputLength && inputBuffer[inputIndex] >= 48 && inputBuffer[inputIndex] <= 57) {
                num = num * 10 + (inputBuffer[inputIndex++] - 48);
        }
        return num * sign;
}

function writeInt(value, isLast = false) {
        if (outputIndex > 0 && !isLast) {
                outputBuffer[outputIndex++] = 32;
        }
        if (value < 0) {
                outputBuffer[outputIndex++] = 45;
                value = -value;
        }
        let digits = [];
        do {
                digits.push(value % 10);
                value = Math.floor(value / 10);
        } while (value > 0);
        for (let i = digits.length - 1; i >= 0; i--) {
                outputBuffer[outputIndex++] = 48 + digits[i];
        }
        if (isLast) {
                outputBuffer[outputIndex++] = 10;
        }
}

// Template Ends here!

// Solution Function

const t = readInt();
for (let i = 0; i < t; i++) {
        let x1 = readInt(),
                y1 = readInt(),
                z1 = readInt();
        let x2 = readInt(),
                y2 = readInt(),
                z2 = readInt();
        let k = readInt();
        
        let dx = Math.abs(x1 - x2);
        let dy = Math.abs(y1 - y2);
        let dz = Math.abs(z1 - z2);
        let [a, b, c] = [dx, dy, dz].sort((p, q) => p - q);
        
        let ans = a + b + c;
        let c1 = Math.ceil(c / k);
        let badTrips = Math.max(0, c1 - 1);
        
        if (badTrips > b + a) {
                let extra = badTrips - (b + a);
                if (extra % 2 === 1) extra++;
                ans += extra;
        }
        
        writeInt(ans, true);
}
fs.writeSync(process.stdout.fd, outputBuffer.slice(0, outputIndex));

Python solution:
from sys import stdin

t = int(stdin.readline())

while t > 0:
    x1, y1, z1, x2, y2, z2, k = map(int, stdin.readline().split())
    
    x_diff = abs(x2-x1)
    y_diff = abs(y2-y1)
    z_diff = abs(z2-z1)
    total_steps = x_diff + y_diff + z_diff
    diff = [x_diff, y_diff, z_diff]
    diff.sort(reverse=True)
    gap = ((diff[0]+k-1)//k)-1
    
    if((diff[1] + diff[2]) >= gap):
        print(total_steps)
    else:
        ext = gap - (diff[1] + diff[2])
        print(total_steps + ext + ext%2)
    
    t -= 1

Ruby solution:
t = gets.to_i

t.times do
  x1, y1, z1, x2, y2, z2, k = gets.split.map(&:to_i)
  x = (x1 - x2).abs
  y = (y1 - y2).abs
  z = (z1 - z2).abs
  ans = x + y + z

  # Calculate adjustments for each coordinate
  v_x = [(x - 1), 0].max / k
  a_x = [v_x - y - z, 0].max
  ans += a_x + (a_x % 2 == 0 ? 0 : 1)

  v_y = [(y - 1), 0].max / k
  a_y = [v_y - x - z, 0].max
  ans += a_y + (a_y % 2 == 0 ? 0 : 1)

  v_z = [(z - 1), 0].max / k
  a_z = [v_z - x - y, 0].max
  ans += a_z + (a_z % 2 == 0 ? 0 : 1)

  puts ans
end