joewiz
11/13/2016 - 6:41 AM

Count down to the holidays, with XQuery

Count down to the holidays, with XQuery

xquery version "3.1";

(: A holiday countdown trinket from a crafts store came with 2 dice for showing how many 
   days left until the holiday. Each die has 6 faces, but the set of numbers on each die 
   is different. Arrange the dice next side by side to indicate the number of days left 
   before the holiday. What numbers are possible? This query lists the combinations.
   Next challenges:
     1. How many days before the holiday does this pair of dice let us begin counting?
        (We don't want any days that we can't.)
     2. Given the date of a holiday date, what actual day can we begin counting?
   Bonus:
     3. Are there any other combinations of dice that would give us a longer countdown?
:)

let $die-1-faces := (0, 1, 2, 3, 4, 5)
let $die-2-faces := (0, 1, 2, 6, 7, 8, (: turn 6 over to become... :) 9)
let $combinations := 
    for $die-1-face in $die-1-faces,
        $die-2-face in $die-2-faces
    return
        (
            $die-1-face || $die-2-face, 
            $die-2-face || $die-1-face
        )
for $combination in distinct-values($combinations)
order by $combination cast as xs:integer
return 
    $combination