-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcups.pas
95 lines (80 loc) · 1.55 KB
/
cups.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
Program cups; {$MODE OBJFPC} {$COPERATORS ON}
Var
Current: Integer;
Next: Array[1..9] of Integer;
MoveNo: Integer;
Function CharToInt(c: Char): Integer;
Begin
Result := Ord(c) - 48
End;
Procedure ReadInput();
Var
i: Integer;
line: String;
Begin
Readln(line);
Current := CharToInt(line[1]);
For i := 1 to 8 do begin
Next[CharToInt(line[i])] := CharToInt(line[i+1])
end;
Next[CharToInt(line[9])] := CharToInt(line[1]);
End;
Procedure PerformMove();
Var
firstPickedUp, destination: Integer;
Begin
firstPickedUp := Next[Current];
destination := Current;
While(True) do begin
destination -= 1;
If(destination < 1) then begin
{
We want to end up with 9, but we do -1 at loop start,
so we have to assign 10 here.
}
destination := 10;
continue
end;
If(
(destination <> firstPickedUp) and
(destination <> Next[firstPickedUp]) and
(destination <> Next[Next[firstPickedUp]])
) then Break
end;
Next[Current] := Next[Next[Next[firstPickedUp]]];
Next[Next[Next[firstPickedUp]]] := Next[Destination];
Next[Destination] := firstPickedUp
End;
Procedure PrintState();
Var
i, now: Integer;
Begin
Write('Cups: ');
now := Current;
For i := 1 to 9 do begin
Write(now, ' ');
now := Next[now]
end;
Writeln('')
End;
Procedure PrintAnswer();
Var
i, now: Integer;
Begin
now := Next[1];
For i := 1 to 8 do begin
Write(now);
now := Next[now]
end;
Writeln('')
End;
Begin
ReadInput();
For MoveNo := 1 to 100 do begin
PrintState();
PerformMove();
Current := Next[Current]
end;
PrintState();
PrintAnswer();
End.