File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
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" ;
You can’t perform that action at this time.
0 commit comments