-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14-2.php
63 lines (56 loc) · 2.11 KB
/
day14-2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
function getReindeerDetails($in){
$list = explode("\n",$in);
$pattern = "/^(.*) can fly ([0-9]*) km\/s for ([0-9]*) seconds, but then must rest for ([0-9]*) seconds\.$/";
$details = array();
foreach($list as $item){
if(!$item){
continue;
}
$matches = array();
preg_match($pattern,$item,$matches);
$details[$matches[1]] = array('speed' => $matches[2],'time' => $matches[3], 'rest' => $matches[4],'inRest'=>false,'curPeriod'=>$matches[3],'curDistance'=>0);
}
return $details;
}
function calcWinner($in, $seconds){
$reindeerDetails = getReindeerDetails($in);
$maxDistance = 0;
$arrPoints = array();
for($x = 0; $x < $seconds; $x++){
$curMax = 0;
$curWinner = array();
foreach($reindeerDetails as $name => $reindeer){
if($reindeer['inRest']){
$reindeerDetails[$name]['curPeriod']--;
if($reindeerDetails[$name]['curPeriod'] <= 0){
$reindeerDetails[$name]['curPeriod'] = $reindeer['time'];
$reindeerDetails[$name]['inRest'] = false;
}
}else{
$reindeerDetails[$name]['curPeriod']--;
$reindeerDetails[$name]['curDistance'] += $reindeer['speed'];
if($reindeerDetails[$name]['curPeriod'] <= 0){
$reindeerDetails[$name]['curPeriod'] = $reindeer['rest'];
$reindeerDetails[$name]['inRest'] = true;
}
}
if($reindeerDetails[$name]['curDistance'] > $curMax){
$curMax = $reindeerDetails[$name]['curDistance'];
$curWinner = array($name);
}elseif($reindeerDetails[$name]['curDistance'] == $curMax){
$curWinner[] = $name;
}
}
foreach($curWinner as $winner){
if(empty($arrPoints[$winner])){
$arrPoints[$winner] = 0;
}
$arrPoints[$winner]++;
}
}
print_r($arrPoints);
}
$seconds = 2503;
//$seconds = 10;
calcWinner(file_get_contents('day14.txt'), $seconds);