SHARE
TWEET

Untitled

a guest Sep 28th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://puzzling.stackexchange.com/questions/89598/generating-roman-numerals-with-dice
  2. // Score checker
  3. // Dmitry Kamenetsky, 28th September 2019
  4.  
  5. import java.util.*;
  6.  
  7. public class RomanNumerals
  8. {
  9.     static int NumDigs=2000;        //number of digits we will consider
  10.     static int N=3;     //number of dice   
  11.  
  12.     static String[] Romans;     //Romans[i] is i-th number in Roman numerals
  13.     static Map<String,Integer> Romans2Dec;
  14.     static boolean[] Seen;
  15.  
  16.     public static void main(String[] args)
  17.     {
  18.         generateRomans();
  19.  
  20.         score("I  II III IV  IX C","V  X  XV  XCV CX C","XX L  LV  LXX C  CXX");            //160 from Bass
  21.         //score("I,II,III,IV,VI,VII","X,XX,XXX,XL,LX,LXX","V,L,IX,LV,XV,XIX");                      //89 from trolley813
  22.         //score("I,II,III,IV,IX,CLXC","V,X,XX,XXX,XL,XXV","V,L,XC,C,CL,CLX");                           //64 from PuzzleGuest
  23.     }
  24.  
  25.     public static void generateRomans()
  26.     {
  27.         char[] digits={'I','V','X','L','C','D','M'};       
  28.         int[][] id={{},{0},{0,0},{0,0,0},{0,1},{1},{1,0},{1,0,0},{1,0,0,0},{0,2}};
  29.  
  30.         Romans=new String[NumDigs+1];
  31.         Romans2Dec=new HashMap<String,Integer>();
  32.  
  33.         for (int i=1; i<=NumDigs; i++)
  34.         {
  35.             String a=""+i;
  36.             String roman="";
  37.  
  38.             for (int k=0; k<a.length(); k++)
  39.             {
  40.                 int digit=(int)(a.charAt(k)-'0');
  41.  
  42.                 for (int q=0; q<id[digit].length; q++)
  43.                     roman+=digits[id[digit][q]+2*(a.length()-1-k)];
  44.             }
  45.  
  46.             //System.out.println(i+" "+roman);
  47.             Romans[i]=roman;
  48.             Romans2Dec.put(roman,i);
  49.         }
  50.     }
  51.  
  52.     public static void score(String dice1, String dice2, String dice3)
  53.     {
  54.         Seen=new boolean[NumDigs+1];
  55.  
  56.         String[][] allDice=new String[N][];
  57.         allDice[0]=dice1.split("[, ]+");
  58.         allDice[1]=dice2.split("[, ]+");
  59.         allDice[2]=dice3.split("[, ]+");
  60.  
  61.         boolean[] done=new boolean[N];
  62.  
  63.         go(done,allDice,"");
  64.  
  65.         for (int i=1; i<=NumDigs; i++)
  66.             if (!Seen[i])
  67.             {
  68.                 System.out.println(i+"="+Romans[i]+" cannot be made");
  69.                 System.out.println("range covered "+(i-1));
  70.                 break;
  71.             }
  72.     }
  73.  
  74.     public static void go(boolean[] done, String[][] allDice, String cur)
  75.     {
  76.         Integer num=Romans2Dec.get(cur);
  77.         if (num!=null) Seen[num]=true;
  78.  
  79.         for (int i=0; i<N; i++)
  80.             if (!done[i])
  81.             {
  82.                 for (int k=0; k<allDice[i].length; k++)
  83.                 {
  84.                     done[i]=true;
  85.                     go(done,allDice,cur+allDice[i][k]);
  86.                     done[i]=false;
  87.                 }
  88.             }
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top