Day 2 finished
This commit is contained in:
30
2023/d2/d2_2.php
Normal file
30
2023/d2/d2_2.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?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;
|
Reference in New Issue
Block a user