-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_1.pas
110 lines (100 loc) · 2 KB
/
part_1.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
program part1;
{ 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] 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 visited: TVisited
);
begin
with guard do begin
while
((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
visited[x, y] := 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;
visited[x, y] := True;
end;
end;
var
visited: TVisited;
obs: TObstacles;
guard: TGuard;
count: integer;
x, y: integer;
begin
for x := 1 to DIM do begin
for y := 1 to DIM do begin
visited[x,y] := False;
obs[x,y] := False;
end;
end;
LoadFile(obs, guard);
RunSimulation(obs, guard, visited);
count := 0;
for x := 1 to DIM do begin
for y := 1 to DIM do begin
if visited[x, y] then count := count + 1;
end;
end;
WriteLn('Total visited: ', count);
end.