-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.php
203 lines (168 loc) · 5.2 KB
/
day7.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
// const INPUT_FILE = 'small.txt';
const INPUT_FILE = 'day7-input.txt';
const ELVES = 5;
const RUN_TIME_OFFSET = 60;
print "Part 1: " . part1() . "\n";
print "Part 2: " . part2() . "\n";
function part1(): string
{
$stepsDone = [];
$rules = getRules();
$stepsRemaining = getDistinctSteps($rules);
while (count($stepsRemaining) > 0) {
$nextStep = getNextEligibleStep($rules, $stepsDone, $stepsRemaining);
// add this rule to the list of applied rules
$stepsDone[] = $nextStep;
// and remove from remaining rules
if (($key = array_search($nextStep, $stepsRemaining)) !== false) {
unset($stepsRemaining[$key]);
}
}
return implode('', $stepsDone);
}
function part2(): int
{
$stepsDone = [];
$schedule = [];
$stepsRunning = [];
$currentTime = 0;
$rules = getRules();
$stepsRemaining = getDistinctSteps($rules);
while (count($stepsRemaining) > 0) {
if (!array_key_exists($currentTime, $schedule)) {
$schedule[$currentTime] = [];
}
// See if any steps have finished running
foreach ($stepsRunning as $step => $endTime) {
if ($endTime === $currentTime) {
unset($stepsRunning[$step]);
$stepsDone[] = $step;
}
}
for ($elf = 0; $elf < ELVES; $elf++) {
// Is this elf available at the current time?
if (!array_key_exists($elf, $schedule[$currentTime])) {
// See if there's a step that can be started
if ($nextStep = getNextEligibleStep($rules, $stepsDone, $stepsRemaining)) {
$endTime = addStepToSchedule($schedule, $currentTime, $elf, $nextStep);
// add this step to the list of running steps
$stepsRunning[$nextStep] = $endTime;
// and remove from list of remaining steps
if (($key = array_search($nextStep, $stepsRemaining)) !== false) {
unset($stepsRemaining[$key]);
}
}
}
}
$currentTime++;
}
// // Debug: print schedule
// $out = '';
// foreach ($schedule as $time => $rows) {
// $out .= $time . "\t";
// for ($elf = 0; $elf < ELVES; $elf++) {
// if (array_key_exists($elf, $schedule[$time])) {
// $out .= $schedule[$time][$elf] . "\t";
// } else {
// $out .= ".\t";
// }
// }
// $out .= "\n";
// }
//
// print $out;
return count($schedule);
}
/**
* @return array
* Array of steps with precedences
* e.g. A => [B, C]
* B => [C, D]
*/
function getRules(): array
{
$rules = [];
$lines = explode("\n", file_get_contents(INPUT_FILE));
$lines = array_map(function (string $line): string {
return str_replace(['Step ', ' must be finished before step ', ' can begin.'], '', $line);
}, $lines);
foreach ($lines as $line) {
if (trim($line)) {
$precedence = $line[0];
$step = $line[1];
$rules[$step][] = $precedence;
}
}
return $rules;
}
/**
* Get the distinct steps from the $rules array
*
* @param array $rules
* Array of letter pairs, e.g. AB, CD
* @return array
* Array of distinct single letters
*/
function getDistinctSteps(array $rules): array
{
$distinctSteps = [];
foreach ($rules as $step => $precedents) {
$distinctSteps[] = $step;
foreach ($precedents as $precedent) {
$distinctSteps[] = $precedent;
}
}
$distinctSteps = array_unique($distinctSteps);
sort($distinctSteps);
return $distinctSteps;
}
/**
* Get a sorted array of steps which can now be applied
*
* @param array $rules
* @param array $stepsDone
* @param array $stepsRemaining
* @return string
* @throws Exception
*/
function getNextEligibleStep(array $rules, array $stepsDone, array $stepsRemaining): string
{
foreach ($stepsRemaining as $step) {
// If we've already done this step, then it's not an eligible next step
if (in_array($step, $stepsDone)) {
continue;
}
// If there are no precedence rules for this step, then this is an eligible step
if (!in_array($step, array_keys($rules))) {
return $step;
}
// If all precedence steps have been done, then this is an eligible step
$allPrecedentsDone = true;
foreach ($rules[$step] as $precedents) {
if (!in_array($precedents, $stepsDone)) {
$allPrecedentsDone = false;
}
}
if ($allPrecedentsDone) {
return $step;
}
}
// No step is eligible at the moment
return '';
}
function addStepToSchedule(array &$schedule, int $currentTime, int $elf, string $step): int
{
$stepRunTime = getStepRunTime($step);
for ($time = $currentTime; $time < $currentTime + $stepRunTime; $time++) {
$schedule[$time][$elf] = $step;
}
return $time;
}
function getStepRunTime(string $step): int
{
$step = strtolower($step);
$index = ord($step);
$runTime = $index - 96;
return RUN_TIME_OFFSET + $runTime;
}