31 lines
		
	
	
		
			607 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			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; |