Short Piles
Source: codechef
Difficulty: 3326

Short Piles
Alice and Bob play a turn‐based game using several piles of stones. There are three types of piles:$X$piles each containing$1$stone,$Y$piles each containing$2$stones, and$Z$piles each containing$3$stones.
The players take turns with
Alice moving first
. On each turn, the current player must remove exactly
one stone
from any non-empty pile.
Alice is not allowed to remove a stone from the same pile on two consecutive turns of hers.
There is no such restriction on Bob’s moves.
Alice’s goal is to remove all the stones from all piles while never violating her rule. Conversely, Bob wins the game if Alice has no legal move on her turn
even though at least one pile is non-empty
.
Determine which player wins if both play optimally.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of three space‐separated integers$X$,$Y$, and$Z$— the number of piles with$1$,$2$, and$3$stones respectively.
Output Format
For each test case, output a single line containing
Alice
if Alice can remove all the stones without ever being forced to make a consecutive move on the same pile, or
Bob
if Bob can force a win.
Constraints$1 \leq T \leq 2\cdot 10^5$$0 \leq X,Y,Z \leq 10^6$$X+Y+Z\geq 1$Sample 1:
Explanation:
Test Case 1
: Here is how the game could have progressed.
Alice chooses the only pile which contains$3$stones and removes$1$stone from it. It now contains$2$stones.
Bob chooses the only pile as well. It now contains$1$stone.
Alice has no legal move, as she cannot take from the same pile that she chose in the first move.
Test Case 2
: With optimal play, Alice can empty all the piles without ever going into a state with no legal move.

Sample Input:
3
0 0 1
0 2 0
1 1 1

Sample Output:
Bob
Alice
Alice

C++ solution:
#include <bits/stdc++.h>

using namespace std;


#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vin(v) for(auto &i : v) cin >> i
#define vout(v) for(auto &i : v) cout << i << ' '; cout<<endl
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define fr(x,y) for(ll i=x;i<y;i++)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define pb push_back
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pll pair<ll, ll>
#define endl '\n'
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
const ll mod = 1e9 + 7;

void solve () {
    ll x,y,z;
    cin >>x>>y>>z;
    if((x==0&&y==0&&z==2)||(z-x)%2==1) {
        cout<<"Bob"<<endl;
    }
    else {
        cout<<"Alice"<<endl;
    }
}

int main() {
    fastio();
    int t=1;
    cin >>t;
    while(t--) {
        solve();
    }
}

Go solution:
package main

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

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

	for i := 0; i < T; i++ {
		scanner.Scan()
		line := scanner.Text()
		parts := strings.Fields(line)
		X, _ := strconv.Atoi(parts[0])
		Y, _ := strconv.Atoi(parts[1])
		Z, _ := strconv.Atoi(parts[2])

		if X == 0 && Y == 0 && Z == 2 {
			fmt.Println("Bob")
		} else if Z > X && (Z+X)%2 == 1 {
			fmt.Println("Bob")
		} else {
			fmt.Println("Alice")
		}
	}
}

Java solution:
import java.util.*;
class Codechef {
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in); // Fixed typo
        int t = sc.nextInt();
        while(t-- > 0){
            long x = sc.nextLong();
            long y = sc.nextLong();
            long z = sc.nextLong();
            boolean alice = true;

            if(x > 0){
                if(x <= z - 1 && ((x + z) & 1) == 1)
                    alice = false;
            } else {
                if((z & 1) == 1){
                    alice = false;
                } else if(z == 2 && y == 0){
                    alice = false;
                }
            }
            System.out.println(alice ? "Alice" : "Bob");
        }
    }
}

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

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

let inputLines = [];
let currentLine = 0;

rl.on('line', (line) => {
    inputLines.push(line);
});

rl.on('close', () => {
    solve();
});

function solve() {
    const T = parseInt(inputLines[0].trim(), 10);
    for (let i = 1; i <= T; i++) {
        const [a, b, c] = inputLines[i].trim().split(' ').map(Number);
        
        if (a === 0 && b === 0 && c === 2) {
            console.log("Bob");
        } else if (c > a && (c + a) % 2 === 1) {
            console.log("Bob");
        } else {
            console.log("Alice");
        }
    }
}

Python solution:
import sys
def main():
    data = sys.stdin.read().split()
    it = iter(data)
    T = int(next(it))
    out = []
    for _ in range(T):
        X = int(next(it)); Y = int(next(it)); Z = int(next(it))
        # Case 1: no 3‑stone piles → Alice wins
        if Z == 0 or X >= Z:
            out.append("Alice")
        else:
            # Now Z > X
            if Z == 1:
                out.append("Bob")
            elif Z == 2:
                # Loses exactly when X==1 or (X==0 and Y==0)
                if X == 1 or (X == 0 and Y == 0):
                    out.append("Bob")
                else:
                    out.append("Alice")
            else:
                # Z >= 3 → parity check
                out.append("Alice" if (Z & 1) == (X & 1) else "Bob")
    sys.stdout.write("\n".join(out))

if __name__ == "__main__":
    main()

Ruby solution:
def main
  input = $stdin.read.split
  idx = 0
  t = input[idx].to_i
  idx += 1
  out = []
  t.times do
    x = input[idx].to_i
    idx += 1
    y = input[idx].to_i
    idx += 1
    z = input[idx].to_i
    idx += 1

    if z == 0 || x >= z
      out << "Alice"
    else
      if z == 1
        out << "Bob"
      elsif z == 2
        if x == 1 || (x == 0 && y == 0)
          out << "Bob"
        else
          out << "Alice"
        end
      else
        if (z & 1) == (x & 1)
          out << "Alice"
        else
          out << "Bob"
        end
      end
    end
  end
  puts out.join("\n")
end

main