-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.php
143 lines (108 loc) · 3.15 KB
/
strategy.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/php
<?php
date_default_timezone_set('UTC');
define('STARTSHIFT', 1);
define('SLEEP', 2);
define('WAKEUP', 3);
class Event {
public $line;
public $type;
public $guardID;
public $dt;
public function __construct($line) {
$brak = strpos($line, ']');
$dt = substr(substr($line, 0, $brak), 1);
$this->dt = new DateTime($dt);
$this->line = $line;
}
}
function str_starts_with($string, $prefix) {
$len = strlen($prefix);
return substr($string, 0, $len) === $prefix;
}
function find_max_ID(&$array) {
$maxID = -1;
$maxValue = -1;
foreach($array as $ID => $value) {
if($value > $maxValue) {
$maxValue = $value;
$maxID = $ID;
}
}
return $maxID;
}
function find_most_sleepy_minute_for_guard($gID) {
global $events;
global $count;
$minutes = [];
for($m = 0; $m < 60; ++$m) $minutes[$m] = 0;
for($i = 0; $i < $count; ++$i) {
$e = $events[$i];
if($e->type != WAKEUP) continue;
if($e->guardID != $gID) continue;
$startMinute = (int)$events[$i-1]->dt->format('i');
$endMinute = (int)$e->dt->format('i');
for($m = $startMinute; $m < $endMinute; ++$m) $minutes[$m] += 1;
}
$maxID = find_max_id($minutes);
return ['id' => $maxID, 'val' => $minutes[$maxID]];
}
$events = [];
while(!feof(STDIN)) {
$line = trim(fgets(STDIN));
if(empty($line)) continue;
$events[] = new Event($line);
}
$count = count($events);
usort($events, function($a,$b){ return $a->dt <=> $b->dt; });
$currentGuardID = -1;
$allGuardIDs = [];
for($i = 0; $i < $count; ++$i) {
$e = $events[$i];
$brak = strpos($e->line, ']');
$line = substr($e->line, $brak+2);
if(str_starts_with($line, 'Guard')) {
$currentGuardID = preg_replace('/[^0-9]/', '', $line);
$currentGuardID = (int)$currentGuardID;
$e->type = STARTSHIFT;
} else if(str_starts_with($line, 'falls')) {
$e->type = SLEEP;
} else if(str_starts_with($line, 'wakes')) {
$e->type = WAKEUP;
} else {
echo "OMG LOL WTF: $line\n";
die;
}
$e->guardID = $currentGuardID;
$allGuardIDs[$currentGuardID] = 1;
}
$allGuardIDs = array_keys($allGuardIDs);
sort($allGuardIDs);
$sleepByGuard = [];
for($i = 0; $i < $count; ++$i) {
$e = $events[$i];
if($e->type != WAKEUP) continue;
$sleepTime = $events[$i-1]->dt->diff($e->dt);
$sleepMinutes = $sleepTime->i;
if(!isset($sleepByGuard[$e->guardID])) $sleepByGuard[$e->guardID] = 0;
$sleepByGuard[$e->guardID] += $sleepMinutes;
}
$mostSleepyGuard = find_max_ID($sleepByGuard);
echo("-> most sleepy guard: #$mostSleepyGuard\n");
$mostSleepyTime = find_most_sleepy_minute_for_guard($mostSleepyGuard)['id'];
echo("-> most sleepy minute: $mostSleepyTime\n");
echo("---> part1 answer: " . ($mostSleepyGuard * $mostSleepyTime) . "\n");
$bestID = $bestMin = $bestVal = -1;
$id_count = count($allGuardIDs);
for($i = 0; $i < $id_count; ++$i) {
$gID = $allGuardIDs[$i];
$minute = find_most_sleepy_minute_for_guard($gID);
echo("-> guard #$gID sleeps " . $minute['val'] . " times on minute " . $minute['id'] . "\n");
if($minute['val'] > $bestVal) {
$bestVal = $minute['val'];
$bestMin = $minute['id'];
$bestID = $gID;
}
}
echo("---> part2: guard #$bestID sleeps $bestVal times on minute $bestMin\n");
echo("---> part2 answer: " . ($bestID * $bestMin) . "\n");