-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.php
157 lines (135 loc) · 4.66 KB
/
day25.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
<?php
include 'intCode.php';
$history = '';
if (isset($_REQUEST['history'])==true) $history = base64_decode($_REQUEST['history']);
$command = '';
if (isset($_REQUEST['command'])==true) $command = trim($_REQUEST['command']);
if (strtolower($command)=='n') $command='north';
if (strtolower($command)=='s') $command='south';
if (strtolower($command)=='w') $command='west';
if (strtolower($command)=='e') $command='east';
// just to show a map on the screen to keep track of rooms easier
$x = 32;
$y = 32;
$x_min = 32;
$x_max = 32;
$y_min = 32;
$y_max = 32;
$records = [];
for ($j=0;$j<65;$j++) {
$records[$j] = [];
for ($i=0;$i<65;$i++) {
$records[$j][$i] = '';
}
}
if (isset($_REQUEST['info'])==true) {
$info = json_decode(base64_decode($_REQUEST['info']),true);
//var_dump($info);
$x = $info['x'];
$y = $info['y'];
$x_min = $info['x_min'];
$x_max = $info['x_max'];
$y_min = $info['y_min'];
$y_max = $info['y_max'];
$entries = explode(',',$info['data']);
$offset = 0;
$records = [];
for ($j=0;$j<65;$j++) {
for ($i=0;$i<65;$i++) {
$records[$j][$i] = $entries[$offset]; $offset++;
}
}
}
$code = file_get_contents(__DIR__ .'/inputs/25.txt');
$robot = new intCodeComputer($code);
$robot->configure(array('pause_output'=>FALSE,'debug'=>FALSE));
echo '<html><head><title>Game</title></head><body style="font-family:courier new;">';
echo '<div style="float:right;width:500px;">'.$history.'</div>';
echo '<p style="font-family:courier new;">';
if (isset($_REQUEST['submitted'])==true) {
$data = base64_decode($_REQUEST['data']);
$robot->import($data);
echo "Sent: <strong>$command</strong>";
if (strlen($command)>0) {
for ($i=0;$i<strlen($command);$i++) {
$robot->input(ord(substr($command,$i,1)));
}
}
$robot->input(10);
} else {
$robot->run();
}
$text = '';
while ($robot->hasOutput()==true) {
$text .= chr($robot->output());
}
// let's try to parse a bit what the output says
if (strpos($text,'==')!==FALSE) {
$pos1 = strpos($text,'==');
$pos2 = strpos($text,'==',$pos1+2);
$room_name = trim(substr($text,$pos1+2,$pos2-$pos1-2));
$directions = '';
$directions .= (strpos($text,'- west')!==FALSE) ? 'W' : '-';
$directions .= (strpos($text,'- north')!==FALSE) ? 'N' : '-';
$directions .= (strpos($text,'- south')!==FALSE) ? 'S' : '-';
$directions .= (strpos($text,'- east')!==FALSE) ? 'E' : '-';
if ($command=='north') $y--;
if ($command=='south') $y++;
if ($command=='west') $x--;
if ($command=='east') $x++;
if ($x_min>$x) $x_min = $x;
if ($x_max<$x) $x_max = $x;
if ($y_min>$y) $y_min = $y;
if ($y_max<$y) $y_max = $y;
$records[$y][$x] = $directions.' '.$room_name;
//echo "Room: $room_name Dir: $directions ".$records[$y][$x] ;
}
$text = str_replace(array(chr(0x0A),chr(0x0D)),"<br/>",$text);
$text = str_replace('<br/><br/>','<br/>',$text);
echo $text;
$text = str_replace('Command?','',$text);
$text = str_replace('<br/><br/>','<br/>',$text);
$history = $text.'<hr/>'.$history;
if ($robot->running==false) echo '<strong>You are dead! Hit Reload to continue.</strong>';
echo '</p>';
echo '<p style="font-size:9px;overflow-x:none;">';
for ($j=$y_min;$j<=$y_max;$j++) {
for ($i=$x_min;$i<=$x_max;$i++) {
if ($i==$x && $j==$y) echo '<span style="font-weight:bold;color:red;">';
echo '[ <span title="'.$records[$j][$i].'">';
echo str_replace(' ',' ',str_pad(substr($records[$j][$i],0,24),24,' '));
echo '</span> ]';
if ($i==$x && $j==$y) echo '</span>';
}
echo '<br/>';
}
echo '</p>';
$data = $robot->export();
// now let's prepare the info stuff to send it back
$info = [];
$x = $info['x'] = $x;
$y = $info['y'] = $y;
$info['x_min']=$x_min;
$info['x_max']=$x_max;
$info['y_min']=$y_min;
$info['y_max']=$y_max;
$entries = '';
for ($j=0;$j<65;$j++) {
for ($i=0;$i<65;$i++) {
$entries .= ','.((isset($records[$j][$i])==true) ? $records[$j][$i] : '');
}
}
$info['data'] = substr($entries,1);
$info_encoded = json_encode($info);
echo '<form name="form" method="post" action="">
<input type="hidden" name="data" value="'.base64_encode($data).'" />
<input type="hidden" name="submitted" value="1" />
<p>Please type your next command:<br/>
Hint: north east south west inv take [item] drop [item]</p>
<input type="text" name="command" value="" autofocus />
<input type="submit" name="go" value="Send" />
<input type="hidden" name="history" value="'.base64_encode($history).'" />
<input type="hidden" name="info" value="'.base64_encode($info_encoded).'" />
</form>';
echo '</body></html>';
?>