#!/usr/bin/env php
<?php
/**
* For each game, determine how many cubes are required in each color. Multiply those numbers together to get the 'power' for a game. Sum the 'power' for all games.
*/
//$input = file_get_contents(__DIR__.'/sample.txt');
$input = file_get_contents(__DIR__.'/input.txt');
$game_lines = explode("\n", trim($input));
$total = 0;
foreach ($game_lines as $line){
$game_parts = explode(": ", $line);
$rounds_text = $game_parts[1];
$game_num = explode(" ",$game_parts[0])[1];
$game_amounts = [];
$rounds = explode('; ', $rounds_text);
// each round is like: 1 red, 10 blue, 5 green
$can_add_game = true;
foreach ($rounds as $round){
$results = explode(", ", $round);
foreach ($results as $result){
$parts = explode(" ", $result);
$color = $parts[1];
$amount = $parts[0];
if ($amount > ($game_amounts[$color] ?? 0 ))$game_amounts[$color] = $amount;
}
}
$total += ($game_amounts['red'] * $game_amounts['green'] * $game_amounts['blue']);
}
echo "Power of all games: $total\n";