Skip to content

Commit a0c24b0

Browse files
committed
2024.06.1
1 parent a0c246d commit a0c24b0

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

php/2024/06/01.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
$start = microtime(true);
4+
5+
#$lines = file('example.txt', FILE_IGNORE_NEW_LINES);
6+
$lines = file('input.txt', FILE_IGNORE_NEW_LINES);
7+
8+
9+
$map = [];
10+
$currentRow = $currentCol = 0;
11+
foreach ($lines as $line) {
12+
if (false !== ($found = strpos($line, '^'))) {
13+
$currentRow = count($map);
14+
$currentCol = $found;
15+
}
16+
$map[] = str_split($line);
17+
}
18+
19+
// N = 0, E = 1, S = 2, W = 3
20+
$directions = [
21+
0 => [0, -1],
22+
1 => [1, 0],
23+
2 => [0, 1],
24+
3 => [-1, 0],
25+
];
26+
$facing = 0;
27+
28+
29+
$visitedPos = [
30+
"{$currentCol}x{$currentRow}" => true,
31+
];
32+
while (true) {
33+
$newCol = $currentCol + $directions[$facing][0];
34+
$newRow = $currentRow + $directions[$facing][1];
35+
36+
if (!isset($map[$newRow][$newCol])) {
37+
break;
38+
}
39+
if ($map[$newRow][$newCol] === '#') {
40+
$facing = ++$facing % 4;
41+
continue;
42+
}
43+
44+
$currentRow = $newRow;
45+
$currentCol = $newCol;
46+
$visitedPos["{$currentCol}x{$currentRow}"] = true;
47+
}
48+
49+
50+
echo count($visitedPos), "\n";
51+
52+
echo microtime(true) - $start;
53+
echo "\n";

php/2024/06/example.txt

132 Bytes
Binary file not shown.

php/2024/06/input.txt

16.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)