-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_2.pas
131 lines (121 loc) · 2.55 KB
/
part_2.pas
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
program part2;
{ FPC 3.2.2 }
{$codepage utf8}
const
DIM = 130;
type
TDir = (TOP, RIGHT, BOTTOM, LEFT);
TGuard = record
x: integer;
y: integer;
dir: TDir;
end;
TObstacles = array[1..DIM, 1..DIM] of boolean;
TVisited = array[1..DIM, 1..DIM, TDir] of boolean;
procedure LoadFile(var obs: TObstacles; var guard: TGuard);
var
f: Text;
x, y: integer;
c: char;
begin
Assign(f, 'input.txt');
Reset(f);
x := 1; y := 1;
while not EOF(f) do begin
Read(f, c);
case c of
'#': obs[x,y] := True;
'^': begin
guard.x := x;
guard.y := y;
guard.dir := TOP;
end;
#10: begin
x := 0;
y := y + 1;
end;
end;
x := x + 1;
end;
Close(f);
end;
procedure RunSimulation(
var obs: TObstacles;
guard: TGuard;
var looped: boolean
);
var
visited: TVisited;
i, j: integer;
begin
looped := False;
for i := 1 to DIM do begin
for j := 1 to DIM do begin
visited[i,j,TOP] := False;
visited[i,j,RIGHT] := False;
visited[i,j,BOTTOM] := False;
visited[i,j,LEFT] := False;
end;
end;
with guard do begin
while
not looped
and ((dir <> TOP) or (y > 1))
and ((dir <> RIGHT) or (x < DIM))
and ((dir <> BOTTOM) or (y < DIM))
and ((dir <> LEFT) or (x > 1))
do begin
if visited[x, y, dir] then looped := True
else visited[x, y, dir] := True;
case dir of
TOP: begin
if obs[x, y - 1] then dir := RIGHT
else y := y - 1;
end;
RIGHT: begin
if obs[x + 1, y] then dir := BOTTOM
else x := x + 1;
end;
BOTTOM: begin
if obs[x, y + 1] then dir := LEFT
else y := y + 1;
end;
LEFT: begin
if obs[x - 1, y] then dir := TOP
else x := x - 1;
end;
end;
end;
end;
end;
var
obs: TObstacles;
guard: TGuard;
looped: boolean;
count: integer;
x, y: integer;
begin
for x := 1 to DIM do begin
for y := 1 to DIM do begin
obs[x,y] := False;
end;
end;
LoadFile(obs, guard);
count := 0;
for x := 1 to DIM do begin
for y := 1 to DIM do begin
{Could be improved by only adding obstructions
to visited places, but my pc is fast enough}
if
not obs[x,y]
and not ((guard.x = x) and (guard.y = y))
then begin
obs[x,y] := True;
RunSimulation(obs, guard, looped);
if looped then count := count + 1;
obs[x,y] := False;
end;
end;
end;
WriteLn('Total possibilities: ', count);
end.