adventofcode/2023/d2/d2_2.php
2023-12-02 15:18:41 +01:00

31 lines
607 B
PHP

<?php
$FILE_NAME = "input.txt";
$sum = 0;
$fd = fopen($FILE_NAME, "r");
while($line = fgets($fd)){
$data = array();
preg_match("/Game (\d+): (.*)/", $line, $data);
$game_id = $data[1];
$hand = explode(";", $data[2]);
$bag = array(
"red" => 0,
"green" => 0,
"blue" => 0,
);
foreach ($hand as $h) {
$colors = array();
preg_match_all("/(\d+) (red|green|blue)/", $h, $colors);
foreach (array_combine($colors[2], $colors[1]) as $key => $value) {
$bag[$key] = max($bag[$key], $value);
}
}
print_r($bag);
$sum += $bag['red'] * $bag['green'] * $bag['blue'];
}
fclose($fd);
echo $sum;