Solving LinkedIn Queens with APL 14 Jun 2025 on Peter Vernigorov’s blog A couple months ago I noticed that LinkedIn now has a few simple games. They’re not much to write home about, but I really enjoy playing Queens. This week I saw two posts about solving the Queens game programmatically. Both were quite interesting to me, so I thought this was a good opportunity to also solve the game in my favourite language - APL - and share my experience. Having been using APL for Advent of Code, I wanted to share my passion for it with others. Rules The game is pretty straightforward: each colored region must have exactly one queen, and two queens cannot occupy the same row, column, or be adjacent. Multiple queens may share diagonals as long as they’re separated by at least one space. Board First, let’s choose a data structure. LinkedIn sends the initial state as a list of rows, assigning each color a number starting from 0. That works for APL too, so let’s create a 2‑dimensional board representing the image: b←⍉⍪0 0 0 0 0 0 0 0 b⍪← 0 0 1 1 1 1 2 0 b⍪← 0 0 1 3 3 1 2 0 b⍪← 0 0 3 3 3 1 2 2 b⍪← 0 4 4 3 1 1 2 2 b⍪← 0 4 5 5 1 6 6 2 b⍪← 4 4 5 5 5 5 6 6 b⍪← 4 4 4 5 7 5 5 6 And print it, by assigning it to the screen represented by a box: ⎕ ← b 0 0 0 0 0 0 0 0 0 0 1 1 1 1 2 0 0 0 1 3 3 1 2 0 0 0 3 3 3 1 2 2 0 4 4 3 1 1 2 2 0 4 5 5 1 6 6 2 4 4 5 5 5 5 6 6 4 4 4 5 7 5 5 6 When solving the puzzle, we’ll use 0 to mark queens, so later in the solution we will increment all numbers by 1 using a simple b + 1 expression. More on that later. Breadth-first Search Next, algorithm choise: depth-first vs breadth-first search. While depth-first is a somewhat obvious choice (and works beautifully for N‑Queens, as seen in this video), I chose breadth-first search, inspired by a Sudoku solution video I love. In our case, the search tree depth equals the number of colors. Each step expands the current solution space by enumerating valid queen positions for a new color. Here’s a simplified mock-up (colo...
First seen: 2025-06-16 08:09
Last seen: 2025-06-16 14:10