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');
$game_lines = explode("\n", trim($input));
// 12 red cubes, 13 green cubes, and 14 blue cubes
$max = [
'red'=>12,
'green'=>13,
'blue'=>14,
];
$total = 0;
foreach ($game_lines as $line){
$game_parts = explode(": ", $line);
$rounds_text = $game_parts[1];
$game_num = explode(" ",$game_parts[0])[1];
$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 > $max[$color])continue 3;
}
}
if ($can_add_game)$total += $game_num;
}
echo "Sum of Game IDs: $total\n";