part2
#!/usr/bin/env php
<?php
/**
* Find numbers on the left that are also on the right. For each match, gain one copy of the next X cards. (If there are four matches, get one copy of each of the next four cards). Process all the cards. Output the total number of cards you processed.
*/
//$input = file_get_contents(__DIR__.'/sample2.txt');
$input = file_get_contents(__DIR__.'/input2.txt');
$expected_output = 13;
//$game_lines = explode("\n", trim($input));
$lines = explode("\n", trim($input));
$total = 0;
function get_num_matches(string $game): int {
$parts = explode(":",$game);
$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);
$num_matches = 0;
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;
$num_matches++;
}
}
//echo "\n Card Value: $card_value";
//echo "\n Num Matches: $num_matches";
return $num_matches;
}
function get_num_subsequent_cards(string $game, int $card_list_size): int {
$parts = explode(":",$game);
$games = trim($parts[1]);
$game_text = trim($parts[0]);
$gn_parts = explode(" ",$game_text);
$game_num = (int)array_pop($gn_parts);
$num_matches = get_num_matches($game);
$num_subsequent_cards = $num_matches;
$game_index = $game_num-1;
$end_index = $game_index + $num_matches;
while ($end_index >= $card_list_size){
$end_index--;
$num_subsequent_cards--;
}
return $num_subsequent_cards;
}
$cards_list = $lines;
//var_dump($cards_list);
//exit;
$stack = $lines;
$card_count = 0;
$loop_count = 0;
$total_cards_generated = 0;
$match_counts = [];
foreach ($cards_list as $index => $card){
$match_counts[$index] = get_num_matches($card);
}
// $solved_matches[199] = 1 bc there are no cards after it ... meaning when you copy card 199, you create 1 card.
$solved_matches = [];
foreach (array_reverse($cards_list, true) as $index=>$card){
echo "\n\n$card";
echo "\n NumMatches: ".get_num_matches($card);
$num_subsequent_cards = get_num_subsequent_cards($card, count($cards_list));
$subsequent_index = $index+1;
$cards_generated = 0;
while ($subsequent_index <= $index + $num_subsequent_cards){
$cards_generated += $solved_matches[$subsequent_index] ?? 1;
$subsequent_index++;
}
$cards_generated += 1;
$total_cards_generated += $cards_generated;
echo "\n Cards Generated: $cards_generated";
$solved_matches[$index] = $cards_generated;
}
//var_dump($solved_matches);
var_dump($total_cards_generated);
echo "\n\nCards Generated: ".$total_cards_generated;
// Solutions: copying card # produces X cards in total
// 6: 1
// 5: 1
// 4: 2
// 3: 4
// 2: 7
// 1: 15
//
//
//
// card 1 has 4 matches
// card 2 has 2 matches
// card 3 has 2 matches
// card 4 has 1 match
// card 5 has no matches
// card 6 is the last card
//
//
// copy card 3:
// +1 card 3
// +1 card 4
// +1 card 5
// +1 card 5
//
// copy card 2:
// +1 card 2
// +1 card 3
// +1 card 4
// +1 card 5
// +1 card 5
// +1 card 4
// +1 card 5