part1
#!/usr/bin/env php
<?php
/**
* Find numbers on the left that are also on the right. The first match is worth 1 point. Each subsequent match doubles the points. Calculate the points of each game. Sum the games together.
*/
//$input = file_get_contents(__DIR__.'/sample1.txt');
$input = file_get_contents(__DIR__.'/input1.txt');
$expected_output = 13;
//$game_lines = explode("\n", trim($input));
$lines = explode("\n", trim($input));
$total = 0;
foreach ($lines as $l){
$parts = explode(":",$l);
$games = trim($parts[1]);
$left = explode("|",$games)[0];
$right = explode("|",$games)[1];
$left_nums = array_map('trim',explode(" ", $left));
$left_nums = array_combine($left_nums, $left_nums);
$right_nums = array_map('trim',explode(" ", $right));
$right_nums = array_combine($right_nums, $right_nums);
$card_value = 0;
echo "\n\n\nLeft Nums:\n";
print_r($left_nums);
echo "\n\n\nRight Nums:\n";
print_r($right_nums);
foreach ($left_nums as $num){
if (!is_numeric($num))continue;
if (isset($right_nums[$num])){
echo "\n Match $num";
if ($card_value == 0) $card_value = 1;
else $card_value = $card_value * 2;
}
}
echo "\n Card Value: $card_value";
$total += $card_value;
}
echo "\nTotal: $total";