From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.36.0.21 with SMTP id 21mr29689697ita.13.1514751287091; Sun, 31 Dec 2017 12:14:47 -0800 (PST) X-Received: by 10.157.90.22 with SMTP id v22mr1703489oth.12.1514751286948; Sun, 31 Dec 2017 12:14:46 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.am4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!g80no4503606itg.0!news-out.google.com!b73ni17387ita.0!nntp.google.com!g80no4503602itg.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 31 Dec 2017 12:14:46 -0800 (PST) In-Reply-To: <1c10eec9-040c-4d50-a557-11325883529a@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:71ca:1cb4:da97:fead; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:71ca:1cb4:da97:fead References: <1c10eec9-040c-4d50-a557-11325883529a@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <79612a29-9183-41dc-b599-ba20e34cd280@googlegroups.com> Subject: =?UTF-8?B?UmU6IEFycmF5cywgc2xpY2VzLCBjYXNlLCBhbmQg4oCYaW7igJkgc3RyYXRlZ2llcw==?= From: Robert Eachus Injection-Date: Sun, 31 Dec 2017 20:14:47 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Body-CRC: 3342331512 X-Received-Bytes: 4446 Xref: reader02.eternal-september.org comp.lang.ada:49713 Date: 2017-12-31T12:14:46-08:00 List-Id: On Friday, December 29, 2017 at 7:51:49 PM UTC-5, Mace Ayres wrote: > I have a two dimensional array, gird, 1..9, 1..9 index of integer and arr= ay of a record type named cell and array is named grid...=20 > I need to check, in addition to whether the proposed ingteger value is al= ready in any cells in that row, or column, whether the proposed value alre= ady > exists in any cell.value field for any cell in triad. The in parameters o= f r,c can reveal what triad is in question, but it looks like some fat code= to to > traverse the abstract 3x3 triads. >=20 > I know I could create some triad types of arrays, but I am wondering if I= can slice the array grid into 9 collections and then check > loop.. if numb (proposed number) already exists in the triad [this would = be determined by row, column in parameters) OR also, the cell/record has a = field with its triad, > a constant property. I would do this by putting the Triad number in each cell, and having a set = type: type Sudoku_Set is array(Integer 1..9) of Boolean; Reset: constant Sudoku_Set :=3D (others =3D> False); Set_Array: array(Integer 1..9) of Sudoku_Set; Row_Array, Col_Array, Triad_Array: Set_Array :=3D (others =3D> Reset); Now your tests for whether a number is already used is some ors: if not (Row_Array(Row) or Col_Array(Column) or Triad_Array(Cell(Row, Column).Triad)(Candidate) then... Oring the three arrays together then chosing a candidate should be faster t= han indexing them separately. I didn't attempt to compile the above so it = probably includes a syntax error or two. The reason I didn't is that once = you have the Ah Ha! moment of oring the sets together, you are likely to ch= ange your tree search to do the oring into a local variable, then do someth= ing like: function Recur(Row, Column: in Integer) is Local_Set: Sudoku_Set :=3D Row_Array(Row) or Col_Array(Column) or Triad_Array(Cell(Row, Column).Triad; begin if Cell(Row, Column) =3D 0 then -- set as part of conditions. if Row < 9 then return Recur(Row+1, Column); elsif Column < 9 then return Recur(1, Column+1); else return True; end if; for I in Integer range 1..9 loop if not Local_Set(I) then Grid(Row_Column).Value :=3D I; Row_Array(Row)(I) :=3D True; Col_Array(Column)(I) :=3D True; end if; =20 if Row < 9 then return Recur(Row+1, Column); elsif Column < 9 then return Recur(1, Column+1); else return True; end if;=20 end loop; end Recur; This does a brute force search, but it should be fast enough. You can add = bells and whistles like filling in a row column or triad cell when eight nu= mbers have been used. I don't think that would make things any faster. Us= ing an access value instead of an index for triads might make it faster, bu= t that's the type of optimization best left until last.