Skip to content

Commit 5efe956

Browse files
committed
makefiles changes & day 16 wip
1 parent 62a4b70 commit 5efe956

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif
3333
runArgs:=$(onlyThisDay) $(onlyThisPart) $(withExamples)
3434
3535
help: ## This help.
36-
@printf "\033[32m---------------------------------------------------------------------------\n Advent of Code 2021 - James Thatcher\n Current Day:\033[33m $(latestDay)\033[32m\n---------------------------------------------------------------------------\033[0m\n"
36+
@printf "\033[32m---------------------------------------------------------------------------\n Advent of Code 2022 - James Thatcher\n Current Day:\033[33m $(latestDay)\033[32m\n---------------------------------------------------------------------------\033[0m\n"
3737
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
3838
3939
.DEFAULT_GOAL := help

src/Days/Day15.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function solvePart2(mixed $input): int|string|null
115115

116116
// check if the point is within the bounds of the grid
117117
if ($x >= 0 && $x <= $max && $y >= 0 && $y <= $max) {
118-
if ($this->isValidPoint($x, $y, $sensors, $max)) {
118+
if ($this->isValidPoint($x, $y, $sensors)) {
119119
// return the tuning frequency
120120
return $x * 4000000 + $y;
121121
}
@@ -133,10 +133,9 @@ public function solvePart2(mixed $input): int|string|null
133133
* @param int $x
134134
* @param int $y
135135
* @param array $sensors sensors with their x, y, and distance to the closest beacon
136-
* @param int $max
137136
* @return bool
138137
*/
139-
protected function isValidPoint(int $x, int $y, array $sensors, int $max): bool
138+
protected function isValidPoint(int $x, int $y, array $sensors): bool
140139
{
141140
foreach ($sensors as $sensor) {
142141
$distance = abs($x - $sensor['x']) + abs($y - $sensor['y']);

src/Days/Day16.php

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,54 @@ class Day16 extends Day
2828
public function solvePart1(mixed $input): int|string|null
2929
{
3030
$input = $this->parseInput($input);
31-
dd($input);
31+
$minute = 0;
32+
$pressure = 0;
33+
$openValves = [];
34+
$valve = $input->first();
35+
$openValve = false;
36+
while ($minute < 30) {
37+
++$minute;
38+
printf("Minute: %d current pressure: %d current valve: %s open valves: %s\n", $minute, $pressure, $valve['valve'], implode(', ', $openValves));
39+
if ($openValve) {
40+
printf("Opening %s releasing %d pressure\n", $openValve['valve'], $openValve['flow_rate']);
41+
$openValves[] = $openValve['valve'];
42+
$valve = $openValve;
43+
$openValve = false;
44+
continue;
45+
}
46+
47+
// otherwise find the next valve with the highest flow rate
48+
$nextValve = $valve['tunnels']->filter(fn (array $tunnel): bool => !in_array($tunnel['valve'], $openValves))->first();
49+
50+
if ($valve['flow_rate'] < $nextValve['flow_rate']) {
51+
printf("Moving to %s\n", $nextValve['valve']);
52+
$valve = $nextValve;
53+
$openValves[] = $valve['valve'];
54+
$open = true;
55+
56+
} else {
57+
printf("Opening %s\n", $valve['valve']);
58+
$openValves[] = $valve['valve'];
59+
$pressure += $valve['flow_rate'];
60+
$valve = null;
61+
}
62+
$pressure += array_sum(array_map(fn (string $valve): int => $input[$valve]['flow_rate'], $openValves));
63+
printf("Minute: %d current pressure: %d current valve: %s open valves: %s\n", $minute, $pressure, $valve['valve'], implode(', ', $openValves));
64+
dd($openValves);
65+
// take the valve with the highest flow rate by shifting and getting the first key
66+
}
67+
68+
dd($pressure);
69+
3270

3371
return null;
3472
}
3573

74+
protected function tunnelWithHighestFlowRate(array $valves, array $openValves): array
75+
{
76+
return $valves->filter(fn (array $valve): bool => !in_array($valve['valve'], $openValves))->first();
77+
}
78+
3679
/**
3780
* Solve Part 2 of the day's problem.
3881
*/
@@ -47,28 +90,34 @@ public function solvePart2(mixed $input): int|string|null
4790

4891
/**
4992
* Parse the input data.
50-
* @return Collection<int, array<string, string|int|array<int, string>>>
93+
*
94+
* @return Collection<string, array<string, int|Collection<int, string>>>
5195
*/
5296
protected function parseInput(mixed $input): Collection
5397
{
54-
$input = is_array($input) ? $input : explode("\n", $input);
55-
56-
return collect($input)
57-
->map(function (string $line): array {
58-
$valve = null;
59-
$flowRate = null;
60-
$tunnels = null;
61-
if (preg_match('/Valve (\w+) has flow rate=(\d+); tunnels? leads? to valves? (.+)/', $line, $matches)) {
62-
$valve = $matches[1];
63-
$flowRate = (int)$matches[2];
64-
$tunnels = explode(', ', $matches[3]);
65-
}
98+
return collect(is_array($input) ? $input : explode("\n", $input))
99+
->mapWithKeys(function (string $line): array {
100+
if (preg_match('/Valve (\w+) has flow rate=(\d+); tunnels? leads? to valves? (.+)/', $line, $matches)) {
101+
[, $valve, $flowRate, $tunnels] = $matches;
66102
return [
67-
'valve' => $valve,
68-
'flow_rate' => $flowRate,
69-
'tunnels' => $tunnels,
103+
$valve => [
104+
'valve' => $valve,
105+
'flow_rate' => (int) $flowRate,
106+
'tunnels' => explode(', ', $tunnels),
107+
]
70108
];
71-
})
72-
;
109+
}
110+
return [];
111+
});
112+
113+
/* return $valves->map(function (array $valve) use ($valves) {
114+
$valve['tunnels'] = collect($valve['tunnels'])
115+
->map(fn (string $tunnel): array => ['valve' => $tunnel, 'flow_rate' => $valves[$tunnel]['flow_rate']])
116+
->sortByDesc(fn (array $tunnel): int => $tunnel['flow_rate'])
117+
//->toArray();
118+
;
119+
120+
return $valve;
121+
}); */
73122
}
74123
}

0 commit comments

Comments
 (0)