text
stringlengths
0
234
if not Line_Bingo and not Column_Bingo then
return False;
end if;
end loop;
return True;
end Is_Bingo;
-- Compute the value of a board according to the exercice.
-- Formula: "sum of all unmarked numbers" x Current_Value
-- @param Board the winning board
-- @param Current_Value The last number called
-- @returns Returns the result of the calculation
function Compute_Board_Value (Board : Grid; Current_Value : Bingo_Values) return Natural;
-------------------------
-- Compute_Board_Value --
-------------------------
function Compute_Board_Value (Board : Grid; Current_Value : Bingo_Values) return Natural is
Sum : Natural := Natural'First;
begin
for Line in Board'Range (1) loop
for Column in Board'Range (2) loop
if not Board (Line, Column).Marked then
Sum := Sum + Board (Line, Column).Value;
end if;
end loop;
end loop;
return Sum * Current_Value;
end Compute_Board_Value;
File : File_Type;
Ball_Box : Queue;
Boards : Vector := Empty_Vector;
Lookup : Map := Empty_Map;
Start_Time, End_Time : CPU_Time;
Execution_Duration : Time_Span;
begin
Get_File (File);
if End_Of_File (File) then
raise Program_Error with "Empty file";
end if;
Fill_Ball_Box : declare
Line : constant String := Get_Line (File);
First : Positive := Line'First;
Last : Positive := Line'First;
Last_Index : Positive := Line'First;
Value : Bingo_Values;
begin
while Last <= Line'Last loop
if Line (Last .. Last) = "," then
Get (Line (First .. Last - 1), Value, Last_Index);
Ball_Box.Enqueue (Value);
Lookup.Insert (Value, Lookup_Item_Vectors.Empty_Vector);
First := Last + 1;
elsif Last = Line'Last then
Get (Line (First .. Line'Last), Value, Last_Index);
Ball_Box.Enqueue (Value);
Lookup.Insert (Value, Lookup_Item_Vectors.Empty_Vector);
First := Last + 1;
end if;
Last := Last + 1;
end loop;
end Fill_Ball_Box;
-- Get all boards
Load_Boards : declare
Line_Index : Bingo_Range := Bingo_Range'First;
Column_Index : Bingo_Range := Bingo_Range'First;
Value : Bingo_Values;
Board : Grid;
begin
while not End_Of_File (File) loop
declare
Line : constant String := Get_Line (File);
Last : Positive := Line'First;
begin
while Last < Line'Last loop
Get (Line (Last .. Line'Last), Value, Last);
Board (Line_Index, Column_Index) := (Value, False);
Update_Lookup (Lookup => Lookup,
Value => Value,
Board_Id => Natural (Boards.Length),
Line => Line_Index,
Column => Column_Index);
if Column_Index = Bingo_Range'Last then
Column_Index := Bingo_Range'First;
if Line_Index = Bingo_Range'Last then
Line_Index := Bingo_Range'First;