part1
#!/usr/bin/env php
<?php
/**
* Add the game ids where 12 red cubese, 13 green cubes, & 14 blue cubes is enough to satisfy the game
*/
//$input = file_get_contents(__DIR__.'/sample.txt');
$input = file_get_contents(__DIR__.'/input.txt');
$expected_output = '';
//$game_lines = explode("\n", trim($input));
$lines = explode("\n", trim($input));
$total = 0;
foreach ($lines as $line_num => $line){
$chars = str_split($line);
$next = $chars[0];
$cur_num = null;
$is_cur_num_valid = false;
$nums = [];
foreach ($chars as $index=>$c){
$next = $chars[$index+1];
if (is_numeric($c)
&& $cur_num === null) $cur_num = $c;
else if (is_numeric($c)) $cur_num .= $c;
if (is_numeric($c) && $is_cur_num_valid == false){
// [line_num, char_num]
//
$positions = [
[$line_num - 1, $index - 1], [$line_num - 1, $index], [$line_num - 1, $index+1],
[$line_num, $index - 1], [$line_num, $index+1],
[$line_num + 1, $index - 1], [$line_num + 1, $index], [$line_num + 1, $index+1],
];
foreach ($positions as $position){
$pos_line = $position[0];
$pos_char = $position[1];
$line = $lines[$pos_line] ?? "";
$char = str_split($line)[$pos_char] ?? null;
if ($line == null || $char == null)continue;
if (!is_numeric($char) && $char != '.')$is_cur_num_valid = true;
}
}
if (!is_numeric($c)){
if ($is_cur_num_valid){
$add = (int)$cur_num;
$total += $add;
}
$cur_num = null;
$is_cur_num_valid = false;
}
}
if ($is_cur_num_valid){
$add = (int)$cur_num;
$total += $add;
}
}
echo "\n\nSum of parts: ". $total."\n\n";