Skip to content

Commit a0c232d

Browse files
committed
2023.16
1 parent a0c2389 commit a0c232d

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

php/2023/16/01.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace Year2023\Day16;
4+
5+
$start = microtime(true);
6+
7+
#$in = file('example.txt', FILE_IGNORE_NEW_LINES);
8+
$in = file('input.txt', FILE_IGNORE_NEW_LINES);
9+
10+
$mirrorMap = [];
11+
foreach ($in as $line) {
12+
$mirrorMap[] = str_split($line);
13+
}
14+
15+
class BeamTracer {
16+
private array $seenCoords = [];
17+
private array $seenCoordAndDirections = [];
18+
public function __construct(private readonly array $mirrorMap)
19+
{}
20+
21+
public function trace(int $x, int $y, string $direction): void
22+
{
23+
do {
24+
$cKey = "$x-$y";
25+
$this->seenCoords[$cKey] = true;
26+
$cdKey = "$x-$y-$direction";
27+
if (isset($this->seenCoordAndDirections[$cdKey])) {
28+
return; // loop detected
29+
}
30+
$this->seenCoordAndDirections[$cdKey] = true;
31+
32+
[$x, $y] = $this->walk($x, $y, $direction);
33+
if (!isset($this->mirrorMap[$y][$x])) {
34+
return;
35+
}
36+
$tile = $this->mirrorMap[$y][$x];
37+
if ($tile === ".") {
38+
continue;
39+
}
40+
41+
if ($tile === '/') {
42+
$direction = match($direction) {
43+
"R" => 'U',
44+
"L" => 'D',
45+
"U" => 'R',
46+
"D" => 'L',
47+
};
48+
continue;
49+
}
50+
51+
if ($tile === '\\') {
52+
$direction = match($direction) {
53+
"R" => 'D',
54+
"L" => 'U',
55+
"U" => 'L',
56+
"D" => 'R',
57+
};
58+
continue;
59+
}
60+
61+
if ($tile === '-') {
62+
if ($direction === 'D' || $direction === 'U') {
63+
$this->trace($x, $y, 'L');
64+
$this->trace($x, $y, 'R');
65+
return;
66+
}
67+
continue;
68+
}
69+
70+
if ($tile === '|') {
71+
if ($direction === 'L' || $direction === 'R') {
72+
$this->trace($x, $y, 'U');
73+
$this->trace($x, $y, 'D');
74+
return;
75+
}
76+
continue;
77+
}
78+
79+
} while (isset($this->mirrorMap[$y][$x]));
80+
}
81+
82+
83+
private function walk (int $x, int $y, string $direction): array
84+
{
85+
return match ($direction) {
86+
"R" => [$x+1, $y],
87+
"L" => [$x-1, $y],
88+
"U" => [$x, $y-1],
89+
"D" => [$x, $y+1],
90+
};
91+
}
92+
93+
public function countCoords(): int
94+
{
95+
return count($this->seenCoords)-1; // the initial tile is out of the map!
96+
}
97+
98+
public function printMap(): void
99+
{
100+
foreach ($this->mirrorMap as $y => $row) {
101+
foreach ($row as $x => $cell) {
102+
if (isset($this->seenCoords["$x-$y"])) {
103+
echo "#";
104+
} else {
105+
echo ".";
106+
}
107+
}
108+
echo "\n";
109+
}
110+
echo "\n";
111+
}
112+
}
113+
114+
$tracer = new BeamTracer($mirrorMap);
115+
$tracer->trace(-1, 0, 'R');
116+
117+
echo "\nPart 1:", $tracer->countCoords(), "\n";
118+
119+
$results = [];
120+
for ($y = 0, $yMax = count($mirrorMap); $y < $yMax; $y++) {
121+
$tracer = new BeamTracer($mirrorMap);
122+
$tracer->trace(-1, $y, 'R');
123+
$results[] = $tracer->countCoords();
124+
125+
$tracer = new BeamTracer($mirrorMap);
126+
$tracer->trace(count($mirrorMap[$y]), $y, 'L');
127+
$results[] = $tracer->countCoords();
128+
}
129+
130+
for ($x = 0, $xMax = count($mirrorMap[0]); $x < $xMax; $x++) {
131+
$tracer = new BeamTracer($mirrorMap);
132+
$tracer->trace($x, -1, 'D');
133+
$results[] = $tracer->countCoords();
134+
135+
$tracer = new BeamTracer($mirrorMap);
136+
$tracer->trace($x, count($mirrorMap), 'U');
137+
$results[] = $tracer->countCoords();
138+
}
139+
140+
echo "Part2: ", max($results), "\n";
141+
142+
echo microtime(true) - $start;
143+
echo "\n";
144+

php/2023/16/example.txt

131 Bytes
Binary file not shown.

php/2023/16/input.txt

11.9 KB
Binary file not shown.

php/common/ArrayKeyCache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function retrieve(array $key): mixed
2222
{
2323
return $this->cache[$this->makeKey($key)] ?? null;
2424
}
25+
26+
public function has(array $key): bool
27+
{
28+
return isset($this->cache[$this->makeKey($key)]);
29+
}
2530
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy