part1

#!/usr/bin/env php
<?php
/** Day 9 Part 1 & 2
 * 
 * My solution fails for part 2. It works for sample2 & sample4, but not sample3 bc of edge cases where a piece is surrounded by pipe, but not actually INSIDE the main loop.
 *
 * I think that would require a complete redesign of the part 2 algo.
 *
 * My algo outputs: 
 * - 736 (too high)
 * I'm trying:
 * - 724 (too high)
 * - 690 (too high)
 * - 600 (incorrect, didn't tell me if high/low)
 */


require(__DIR__.'/functions.php');

if (@$argv[1] == 'sample'){
    $input = file_get_contents(__DIR__.'/sample.txt');
} else if (@$argv[1] == 'sample2'){
    $input = file_get_contents(__DIR__.'/sample2.txt');
}  else if (@$argv[1] == 'sample3'){
    $input = file_get_contents(__DIR__.'/sample3.txt');
}  else if (@$argv[1] == 'sample4'){
    $input = file_get_contents(__DIR__.'/sample4.txt');
} else {
    $input = file_get_contents(__DIR__.'/input.txt');
}

//$lines = explode("\n", trim($input));
//$lines = array_map('trim', $lines);

$line_width = strpos($input,"\n");
$s_pos = strpos($input, "S");
$s_y = floor($s_pos / $line_width);
$s_x = $s_pos % ($line_width+1);

echo "\nLine Width: $line_width";
echo "\nstr_pos: $s_pos";
echo "\nS cord: $s_x,$s_y";
//echo "\nsY: $s_y";
//echo "\nsX: $s_x";

$s = (object)['x'=>$s_x, 'y'=>$s_y];

$connecting_pipes = find_connecting_pipes($s,$line_width,$input);

$cur_step = $s;
$next_step = $connecting_pipes[0]; 
$prev_step = $connecting_pipes[1];
$num_steps_away = 1;
//

$pipe_cords = [$s,$next_step,$prev_step];

// until we're at the farthest-away-position, we need to keep getting next steps
// in both directions
// We know we're at the farthest-away-position when the next-step is the same as the prev-step OR the prev-step's next step.

$from_next = $s;
$from_prev = $s;

echo "\nFound Pos: ".$s->x.','.$s->y;
echo "\nNext Pos: ".$next_step->x.','.$next_step->y;
echo "\nPrev Pos: ".$prev_step->x.','.$prev_step->y;
echo "\n\n";

while (true){


    echo "\nAlready Searched in 'next': ".$from_next->x.','.$from_next->y;
    echo "\nSearch around next pos: ".$next_step->x.','.$next_step->y;

    echo "\nAlready Searched in 'prev': ".$from_prev->x.','.$from_prev->y;
    echo "\nSearch around prev pos: ".$prev_step->x.','.$prev_step->y;
    echo "\n\n";


    $new_next = get_next_step($from_next, $next_step, $line_width, $input);
    if ($new_next == $from_prev)break;
    $pipe_cords[] = $new_next;

    $new_prev = get_next_step($from_prev, $prev_step, $line_width, $input);


    $from_next = $next_step;
    $next_step = $new_next;

    $from_prev = $prev_step;
    $prev_step = $new_prev;

    $num_steps_away++;

    //$from_next = $next_step;
    //$from_prev = $prev_step;

    if ($from_next == $from_prev)break;
    $pipe_cords[] = $new_prev;
}

$last = array_pop($pipe_cords);
$lastagain = array_pop($pipe_cords);
if ($last == $lastagain)$pipe_cords[] = $last;
else {
    $pipe_cords[] = $last;
    $pipe_cords[] = $lastagain;
}

$PIPE = 7;
$INSIDE = 12;
$OUTSIDE = 15;
$SEARCHING = 56;
$UNKNOWN = -1;

$xycords = [];
foreach ($pipe_cords as $cord){
    $n = $cord->x.','.$cord->y;
    $xycords[$n] = $PIPE;
}

unset($pipe_cords);
echo "\n\nJust the pipes:\n";
print_r($xycords);

$lines = explode("\n", trim($input));

$x_max = $line_width - 1;
$y_max = count($lines) - 1;

foreach ($lines as $y => $line){
    foreach (str_split($line) as $x => $char){
        $n = "$x,$y";
        if (isset($xycords[$n]))continue;
        $xycords[$n] = get_char_type($x,$y, $xycords, $x_max, $y_max);
    }
}

$unknown_list = [];

foreach ($xycords as $cord=>$type){
    if ($type==$UNKNOWN)$unknown_list[$cord] = $type;
}


//echo "\n\n\n";

//print_r($xycords);

//echo "\n\n";

//echo "\n\n\n\n--------\n\n";

//$_57type = get_char_type(5,7,$xycords, $x_max, $y_max);

//echo "\n\n";

//var_dump($_57type);



//echo "\n\n\n";

//exit;

$has_changes = true;
$i =0;
while (count($unknown_list) > 0 && $has_changes){
    $i++;
    $has_changes = false;
    foreach ($unknown_list as $cord=>$type){
        $parts = explode(",", $cord);
        $x = $parts[0];
        $y = $parts[1];
        $new_type = get_char_type($x,$y,$xycords, $x_max, $y_max);
        var_dump($new_type);
        if ($new_type!=$UNKNOWN && $new_type != $SEARCHING){
            //echo "WHAT";
            $xycords[$cord] = $new_type;
            unset($unknown_list[$cord]);
            $has_changes = true;
        } else {
            $xycords[$cord] = $UNKNOWN;
            $unknown_list[$cord] = $UNKNOWN;
        }
    }

}


echo "\n\nAll xycords:\n";
print_r($xycords);


echo "\n\n";


$inside_count = 0;
foreach ($xycords as $cord=>$type){
    if ($type==$UNKNOWN)$inside_count++;
}

echo "\n\nCount inside pipes: ".$inside_count;

echo "\n\n";


$out_chars = [
    $UNKNOWN => '?',
    $INSIDE => 'i',
    $OUTSIDE => '.',
    $PIPE => '+',

];


$lines = [];
foreach ($xycords as $cord=>$type){
    $parts = explode(",", $cord);
    $x = $parts[0];
    $y = $parts[1];

    $out = $out_chars[$type];
    $lines[$y][$x] = $out;
}

$output = "";
ksort($lines);
foreach ($lines as $line){
    ksort($line);
    $output .= "\n".implode("",$line);
}

echo "\n\n$output\n\n";

echo "\n\n\n\n\n\n\n";

// in sample 3, I'm failing to mark:
// - 4,4 as outside
// - 3,2 as outside