-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-crab-million-cups.pas
81 lines (69 loc) · 1.51 KB
/
one-crab-million-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
Program cups; {$MODE OBJFPC} {$COPERATORS ON}
Const
MILLION = 1000 * 1000;
Var
Current: Integer;
Next: Array[1..MILLION] 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])] := 10;
For i := 10 to MILLION-1 do begin
Next[i] := i+1
end;
Next[MILLION] := 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 MILLION, but we do -1 at loop start,
so we have to assign (MILLION + 1) here.
}
destination := MILLION + 1;
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 PrintAnswer();
Var
first, second: Integer;
Begin
first := Next[1];
second := Next[first];
Writeln(first, ' * ', second, ' = ', first * second)
End;
Begin
ReadInput();
For MoveNo := 1 to (10 * MILLION) do begin
PerformMove();
Current := Next[Current]
end;
PrintAnswer();
End.