<?php

$input = file_get_contents('./input.txt');

preg_match('/^target area: x=(\d+)\.\.(\d+), y=(-?\d+)\.\.(-?\d+)$/', $input, $matches);

[, $x_min, $x_max, $y_min, $y_max] = $matches;
$target = ['x_min' => $x_min, 'x_max' => $x_max, 'y_min' => $y_min, 'y_max' => $y_max];

function increment(&$x, &$y, &$x_velocity, &$y_velocity)
{
    $x += $x_velocity;
    $y += $y_velocity;
    if ($x_velocity > 0) $x_velocity -= 1;
    elseif ($x_velocity < 0) $x_velocity += 1;
    else $x_velocity = 0;
    $y_velocity -= 1;
}

function check($x, $y, $target)
{
    return $x >= $target['x_min'] &&
        $x <= $target['x_max'] &&
        $y >= $target['y_min'] &&
        $y <= $target['y_max'];
}

$possibilities = [];
for ($i = 0; $i <= abs($target['x_max']); $i++) {
    for ($j = -abs($target['y_min']); $j <= abs($target['y_min']); $j++) {
        if ($i === 0 && $j === 0) continue;
        $x = 0;
        $y = 0;
        $x_velocity = $i;
        $y_velocity = $j;
        while(!check($x, $y, $target)) {
            echo sprintf('x: %s, y: %s%s', $x, $y, "\n");
            increment($x, $y, $x_velocity, $y_velocity);
            if ($y <= $target['y_min']) {
                break;
            }
        }
        if (check($x, $y, $target)) {
            $possibilities[] = sprintf('%s,%s', $i, $j);
        }
    }
}

echo count($possibilities);